循環嵌套和break語句
while、do while和for語句都可以形成嵌套,在這里我們着重分析for語句的嵌套。
程序1
簡單的for語句嵌套
// 20-1簡單for語句嵌套.c #include <stdio.h> //main() //{ // int i, j,k; // k = 0; // for (i = 0; i < 3; i++) // { // for (j = 0; j < 3; j++) // { // printf("%d\n", ++k); // } // } //} //二維數組 main() { int i, j, k; k = 0; for (i = 0;i < 3;i++) { for (j = 0;j < 3;j++) { printf("%d", j); } printf("\n"); } }
程序2
打印九九乘法表
// 20-2打印九九乘法表.c #include <stdio.h> main() { int num1, num2; //簡易九九乘法表 for (num1 = 1;num1<10;num1++) { for (num2 = 1; num2 < 10; num2++) { printf("%d*%d=%d\t", num1, num2, num1*num2); if (num1 == num2) break; } printf("\n"); } printf("\n"); }
注意:break語句不是跳出if中的花括號,而是直接跳出本層循環
程序3
小游戲:打飛碟
// 20-3綜合程序打飛碟.c // #include <stdio.h> #include <Windows.h> void gotoxy(int x, int y); //獲取坐標函數 main() { int x = 0, y = 0, x1 = 37, y1 = 22, x2 = 40, y2 = 18; //飛碟坐標 炮台的坐標 炮彈坐標 int c = 0; //獲取鍵盤值 while (c != '\r') //沒有 按回車鍵 { if (_kbhit()) //檢測鍵盤按鍵 c = _getch(); //獲取按鍵字符 system("cls"); //清楚屏幕 //飛碟成像 x++; if (x > 70) x = 0; gotoxy(x, y); printf("@@@@@@@@"); //炮台成像 gotoxy(x1,y1); printf("_^_"); if (c == ' ') //如果鍵盤符是 空格 { //炮彈成像 y2--; if (y2 < 0) { c = '\0'; //鍵盤按鈕復位 y2 = 18; //炮彈復位 } gotoxy(x2,y2); printf("^\n\t\t\t\t\t^\n\t\t\t\t\t^\n\t\t\t\t\t"); //檢測打中目標 if (x<x2 && x+10>x2 && y-2<y2 && y+2>y2) { printf("恭喜過關\n"); break; } } Sleep(100); //延時 } } void gotoxy(int x, int y) //獲取坐標函數 { HANDLE houtput; COORD loc; loc.X = x; loc.Y = y; houtput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(houtput, loc); }
求e的值。
(1)用for循環,計算前50項
(2)用while循環,要求直至最后一項的值小於10-4
//20_1:求e的值,e=1+1/1!+1/2!+1/3!...+1/n! //(1)用for循環,計算前50項*/ #include <stdio.h> main() { long i; //計數器 double factorial = 1.0, sum = 1.0; //階乘 總和 //循環五十次 for (i = 1; i <= 50; i++) { factorial *= i; //計算階乘 sum = sum + 1 / factorial; //總和 } printf("%lf\n", sum); }
//(2)用while循環,要求直至最后一項的值小於10的-4次方 #include <stdio.h> main() { long i = 1; // i 做乘數,不能為0 double factorial = 1.0, sum = 1.0; //階乘 總和 while (1/ factorial > 1e-4) //通過比較判斷是否循環 { //printf("%lf\n", (1 / factorial)); factorial *= i; //階乘 sum = sum + 1 / factorial; //和 i++; //printf("%ld\n", i); } printf("%lf\n", sum); }
給一個不多於5位的正整數,要求:
(1)求出它是幾位數;
(2)分別打印出每一位數字;
(3)按逆序打印出各位數字,例如原數為487,應輸出784。
//(1)求出它是幾位數; #include <stdio.h> main() { //求出是幾位數 int a, i, j; //輸入的值 幾位數 增值中間使用 scanf_s("%d", &a); //輸入值 j = 1; for ( i = 0; i < 5; i++) //循環五次 { if (a/j == 0)break; //利用整數取值 如: 366 / 1 = 366; 366 / 10 = 36; 36 / 100 = 0; i = 3 j *= 10; } printf("%d是%d位數", a, i); }
//(2)分別打印出每一位數字; #include <stdio.h> main() { //求出是幾位數 int a, i, j, n[5] = {0}; //輸入的值 幾位數 增值中間使用 保存值到數組 scanf_s("%d", &a); //輸入值 j = 10; for (i = 0; i < 5; i++) //循環五次 { n[i] = a % j; //將整數的余數取出來 a /= j; //刪除取出來的數 if (a == 0)break; // a = 0 時,數據已取完,直接跳出循環。 } for (i = 4; i >= 0; i--) //數組中存的是:比如輸入的是 456 n[5] = {6,5,4,0,0} if (n[i] != 0)printf("%d", n[i]); //因此要從大到小循環 }
//(3)按逆序打印出各位數字,例如原數為487,應輸出784。 #include <stdio.h> main() { //求出是幾位數 int a, i, j, n[5] = { 0 }; //輸入的值 幾位數 增值中間使用 保存值到數組 scanf_s("%d", &a); //輸入值 j = 10; for (i = 0; i < 5; i++) //循環五次 { n[i] = a % j; //將整數的余數取出來 a /= j; //刪除取出來的數 if (a == 0)break; // a = 0 時,數據已取完,直接跳出循環。 } for (i = 0; i < 5; i++) //數組中存的是:比如輸入的是 456, n[5] = {6,5,4,0,0} if (n[i] != 0)printf("%d", n[i]); //因此直接打印即可 }