使用for循环打印出X模式中的字符 [英] Printing out characters in an X pattern using for loops
本文介绍了使用for循环打印出X模式中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
xx
xx
xx
xx
x
xx
xx
xx
xx
我无法弄清楚算法。
到目前为止,我已经是
#include< stdio.h>
int main()
{
int j,i;
char ch [] =$; (i = 5; i> = 0; - i)
{
(int j = 5; j> = 0; - j)
{
if(j%2 == 1)
{
printf(%s,ch);
}
}
printf(\\\
);
}
return 0;
}
我也想出了这个模式,
0 5 0
1 3 1
2 1 2
3 0 3
2 1 2
1 3 1
0 5 0
边上的数字是a * 。中间的数字是空格应该出现的地方。我缺少什么?
解决方案
int main(void)
{
int i;
int w = 7;
for(i = 1; i <= w *(w + 1); i ++)
printf(
%c,
i%(w + 1)== 0?'\\\
':
i%(w)== 0 || i%(w + 2)== 1?'*':'');
}
I'm trying to print this big X pattern:
x x
x x
x x
x x
x
x x
x x
x x
x x
I can't figure out the algorithm to make it. I'm just starting C programming and getting stuck.
What I have so far is
#include <stdio.h>
int main()
{
int j,i;
char ch[] = "$";
for(int j = 5; j >= 0 ; --j)
{
for(i = 5; i>=0; --i)
{
if(j%2 == 1)
{
printf("%s",ch);
}
}
printf("\n");
}
return 0;
}
I figured out the pattern too,
0 5 0
1 3 1
2 1 2
3 0 3
2 1 2
1 3 1
0 5 0
The numbers on the edge are where a * should appear. The numbers in the middle are where spaces should appear. What am I missing?
解决方案
For a little bit of fun:
int main (void)
{
int i;
int w = 7;
for (i=1;i<=w*(w+1);i++)
printf (
"%c",
i % (w+1) == 0 ? '\n' :
i % (w) == 0 || i % (w+2) == 1 ? '*' : ' ');
}
这篇关于使用for循环打印出X模式中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文