第 6 章 C控制语句:循环
程序员文章站
2022-06-23 23:28:24
6.16.3 使用嵌套循环,按下面格式打印字母: F FE FED FEDC FEDCB FEDCBA 1 #include 2 3 int main() 4 { 5 const int ROWS = 6; 6 7 for (int row(0); row != ROWS; ++ ......
6.16.3 使用嵌套循环,按下面格式打印字母:
F
FE
FED
FEDC
FEDCB
FEDCBA
1 #include <stdio.h> 2 3 int main() 4 { 5 const int ROWS = 6; 6 7 for (int row(0); row != ROWS; ++row) 8 { 9 for (char ch('F'); ch >= 'F' - row; --ch) 10 printf("%c",ch); 11 12 printf("\n"); 13 } 14 15 return 0; 16 }
6.16.4 使用嵌套循环,按下面格式打印字母:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
1 #include <stdio.h> 2 3 int main() 4 { 5 const int ROWS = 6; 6 const int COLS = 6; 7 char ch = 'A'; 8 9 for (int row(0); row != ROWS; ++row) 10 { 11 for (int col(0); col <= row; ++col, ++ch) 12 printf("%c", ch); 13 14 printf("\n"); 15 } 16 17 return 0; 18 }
6.16.5
1 #include <stdio.h> 2 3 int main() 4 { 5 printf("Input a letter: "); 6 7 char ch; 8 while (1 == scanf("%c", &ch)) 9 { 10 if ('\n' == ch || ' ' == ch || '\t' == ch) 11 continue; 12 13 if (ch < 'A' || ch > 'z') 14 printf("\nIt's not a letter, try again: "); 15 else 16 break; 17 } 18 19 if (ch >= 'a') ch -= 'a' - 'A'; 20 21 const int ROWS = ch - 'A' + 1; 22 const char *SPACE = " "; 23 24 for (int row(0); row != ROWS; ++row) 25 { 26 for (int col(0); col != ROWS - row; ++col) 27 printf(SPACE); 28 29 char chTmp = 'A'; 30 for (; chTmp != 'A' + row; ++chTmp) 31 printf("%c", chTmp); 32 33 for (; chTmp != 'A' - 1; --chTmp) 34 printf("%c", chTmp); 35 36 printf("\n"); 37 } 38 39 return 0; 40 }
上一篇: 【js】【图片瀑布流】js瀑布流显示图片20180315
下一篇: css实现3D切换功能