C 打印出这个星形的问题
在练习过程中,我遇到一项作业,要求我编写一个代码,打印出以下形状: 我尝试了一种有两个for循环的方法。我知道打印星星形状的基本原则,但我就是不能完成这一条。无论我做什么,我都可以打印出形状的左部分(左金字塔),但右部分最终会“扭曲”C 打印出这个星形的问题,c,C,在练习过程中,我遇到一项作业,要求我编写一个代码,打印出以下形状: 我尝试了一种有两个for循环的方法。我知道打印星星形状的基本原则,但我就是不能完成这一条。无论我做什么,我都可以打印出形状的左部分(左金字塔),但右部分最终会“扭曲” #包括 #包括 int main() { int n,i,j; printf(“输入n:”); scanf(“%d”和“&n”); 整数长度=2*n-1,宽度=2*n+1; 对于(i=0;i而不是到处循环并过滤以确定要打印哪个字符(正如您所注意到的,这是一个非常
#包括
#包括
int main()
{
int n,i,j;
printf(“输入n:”);
scanf(“%d”和“&n”);
整数长度=2*n-1,宽度=2*n+1;
对于(i=0;i而不是到处循环并过滤以确定要打印哪个字符(正如您所注意到的,这是一个非常棘手的问题),请将图分解为多个部分
首先打印一行,width
width和stars
stars
这可以通过三个循环完成:
void print_line(int width, int stars)
{
for (int i = 0; i < stars; i++)
printf("*");
for (int i = 0; i < width-2*stars; i++)
printf(" ");
for (int i = 0; i < stars; i++)
printf("*");
printf("\n");
}
这种方式有效:
#include <stdio.h>
int main()
{
int n,i,j,k;
printf("Enter n: ");
scanf("%d", &n);
int length=2*n-1;
int reqd_no_of_stars;
for(i=0; i<=length; i++)
{
if(length+1-i<i)
{
reqd_no_of_stars=length+1-i;
}
else
{
reqd_no_of_stars=i;
}
for(k=0; k<reqd_no_of_stars; k++)
printf("*");//print left set of stars equal to the number of reqd_no_of_stars
for(k=0; k<(2*n+1)-2*reqd_no_of_stars; k++)
printf(" ");//print required spaces=total_width-required_width_for_stars=((2*n+1)-2*reqd_no_of_stars) in between stars of both sides
for(k=0; k<reqd_no_of_stars; k++)
printf("*");//print right set of stars equal to the number of reqd_no_of_stars
printf("\n");
}
return 0;
}
#包括
int main()
{
int n,i,j,k;
printf(“输入n:”);
scanf(“%d”和“&n”);
整数长度=2*n-1;
国际所需星号;
对于(i=0;i如果构建代码,则在绘制关系时几乎没有问题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char AST[] = "**********";
static const char BLK[] = " ";
void paint_line(const char *s, int n)
{
int l = strlen(s);
while (n >= l) {
fputs(s, stdout);
n -= l;
}
/* n < l */
if (n > 0)
printf("%.*s", n, s);
}
void paint_tie(int n)
{
int row;
for(row = 1; row <= n; row++) {
paint_line(AST, row);
paint_line(BLK, 2*(n-row) + 1);
paint_line(AST, row);
printf("\n");
}
for (row = n-1; row > 0; row--) {
paint_line(AST, row);
paint_line(BLK, 2*(n-row) + 1);
paint_line(AST, row);
printf("\n");
}
}
int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
int n = atoi(argv[i]);
paint_tie(n);
}
}
#包括
#包括
#包括
静态常量字符AST[]=“**********”;
静态常量字符BLK[]=“”;
无效绘制线(常量字符*s,整数n)
{
int l=strlen(s);
而(n>=l){
FPUT(s、stdout);
n-=l;
}
/*n0)
printf(“%s”,n,s);
}
无效油漆接头(内部n)
{
int行;
对于(行=1;行0;行--){
油漆线(AST,世界其他地区);
油漆线(黑色,2*(n行)+1);
油漆线(AST,世界其他地区);
printf(“\n”);
}
}
int main(int argc,字符**argv)
{
int i;
对于(i=1;i
例程main()
只是从命令行中提取领带的尺寸。我没有包含检查atoi(3)
是否返回正确值的代码,但请确保您可以完成此练习
例程paint_tie()
只打印一个tie,它只打印带有请求字符数的行,增长到n
,然后又变为零
最后,例程paint\u line
只绘制一个字符序列,从作为参数传递的字符串中选择。它使用函数fputs(3)
打印尽可能多的完整字符串(只是为了提高效率),直到没有多余的位置容纳完整字符串,然后使用(只打印一次,因为这很昂贵)%.*s
格式,它允许我获取字符串的第一部分,直到尾随字符不是字符串长度的倍数。您可以下载示例
顺便问一下,你为什么问这个?这是一个家庭作业练习,还是你只是在练习(正如你在问题中所说的)在任何情况下,你在这里提问都是不好的,因为你没有锻炼自己。为什么你会自动要求做一个练习,然后来这里寻找答案?我不明白。这是一种问题,使用调试器可以让你更好地了解问题所在。每行的总长度应该是1+n*2打印I星< /COD>,<代码>(1 +N*2)-i*2个空格
然后再次为每个迭代/行打印i个星号
。所需的形状是纯文本,没有插图。无需使用指向文本图像的链接,而您可以简单地将文本直接复制粘贴到问题中。@Gerhardh所以您自动假设我没有尝试复制和粘贴文本,对吗?我不太确定是什么原因你想用一个对手头的问题没有帮助的评论来实现,但是如果你仍然感兴趣,我想让你知道,我不太确定当通过手机或不同的计算机查看问题时,文本格式是否会改变文本,粘贴链接是我发现的唯一可靠的传达方式我想传达的信息。我不认为你没有尝试,但至少人们并不热衷于关注问题中的任何链接。包含所有必需信息的问题非常感谢。有些人只是忽略了需要关注链接的问题。你可以编辑你的问题以包括测试(格式化为代码)为了吸引更多的读者。谢谢你的帮助,这是最有价值的。至于你的问题,你实际上是自己回答了第一部分。如果你仔细看,你可能会注意到。一般来说,你认为我没有锻炼自己,这不仅是不必要的,而且是非常大胆的。我真诚地希望你理解而且,你不可能知道我花了多少时间做这个练习都没有用。这不是几个小时的问题,而是好几天的问题。
#include <stdio.h>
int main()
{
int n,i,j,k;
printf("Enter n: ");
scanf("%d", &n);
int length=2*n-1;
int reqd_no_of_stars;
for(i=0; i<=length; i++)
{
if(length+1-i<i)
{
reqd_no_of_stars=length+1-i;
}
else
{
reqd_no_of_stars=i;
}
for(k=0; k<reqd_no_of_stars; k++)
printf("*");//print left set of stars equal to the number of reqd_no_of_stars
for(k=0; k<(2*n+1)-2*reqd_no_of_stars; k++)
printf(" ");//print required spaces=total_width-required_width_for_stars=((2*n+1)-2*reqd_no_of_stars) in between stars of both sides
for(k=0; k<reqd_no_of_stars; k++)
printf("*");//print right set of stars equal to the number of reqd_no_of_stars
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char AST[] = "**********";
static const char BLK[] = " ";
void paint_line(const char *s, int n)
{
int l = strlen(s);
while (n >= l) {
fputs(s, stdout);
n -= l;
}
/* n < l */
if (n > 0)
printf("%.*s", n, s);
}
void paint_tie(int n)
{
int row;
for(row = 1; row <= n; row++) {
paint_line(AST, row);
paint_line(BLK, 2*(n-row) + 1);
paint_line(AST, row);
printf("\n");
}
for (row = n-1; row > 0; row--) {
paint_line(AST, row);
paint_line(BLK, 2*(n-row) + 1);
paint_line(AST, row);
printf("\n");
}
}
int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
int n = atoi(argv[i]);
paint_tie(n);
}
}