FOR循环中的嵌套循环!!
本人新手 自学C 学到for循环 要用嵌套循环打出下面的符号$
$$
$$$
$$$$
$$$$$
和
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
我想了几个小时都想不出来 请问下各位大侠应该怎么写额 谢谢了!
#include "stdio.h" int main() { int i,j,k,m; for (i=1;i<=26;i++) { for (m=40;m-i>0;m--) printf(" "); for (j=0;j<i;j++) printf("%c",'A'+j); for (k=j-1;k>0;k--) printf("%c",'A'+k-1); printf("\n"); } }第二个我再引申下,输出为:
A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA ABCDEFGFEDCBA ABCDEFGHGFEDCBA ABCDEFGHIHGFEDCBA ABCDEFGHIJIHGFEDCBA ABCDEFGHIJKJIHGFEDCBA ABCDEFGHIJKLKJIHGFEDCBA ABCDEFGHIJKLMLKJIHGFEDCBA ABCDEFGHIJKLMNMLKJIHGFEDCBA ABCDEFGHIJKLMNONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA
#include "stdio.h" #include "math.h" #define N 11 /*这个常数你可以自己修改,1~26都很有意思 */ #define f(i) (i>N?(2*N-i):i) int main() { int i,j,k,m; for (i=1;i<=2*N-1;i++) { for (m=40;m-f(i)>0;m--) printf(" "); for (j=0;j<f(i);j++) printf("%c",'A'+j); for (k=j-1;k>0;k--) printf("%c",'A'+k-1); printf("\n"); } }输出为:
A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA ABCDEFGFEDCBA ABCDEFGHGFEDCBA ABCDEFGHIHGFEDCBA ABCDEFGHIJIHGFEDCBA ABCDEFGHIJKJIHGFEDCBA ABCDEFGHIJIHGFEDCBA ABCDEFGHIHGFEDCBA ABCDEFGHGFEDCBA ABCDEFGFEDCBA ABCDEFEDCBA ABCDEDCBA ABCDCBA ABCBA ABA A