循環嵌套
循環嵌套的使?
while , do while , for ,這三種循環往往會嵌套在?起才能更好的解決問題,就是我們所說的:循環嵌套。這三種循環都可以任意嵌套使?
?如:
寫?個代碼,打印?個乘法?訣表
1*1= 1
1*2= 2 2*2= 4
1*3= 3 2*3= 6 3*3= 9
1*4= 4 2*4= 8 3*4=12 4*4=16
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
- 打印的內容是分為9?的,第1?打印1項,第2?打印2項,第3?打印3項,以此類推,第9?打印9項。
- 每??的信息,每?項的第?個乘數和列號是?致的,每?項的第?個乘數和?號是?致的,兩個乘數的積就是第三個數。
- 打印9?,我們可以使?循環解決,總共循環9次,每次進?循環打印??,循環變量使? i 來控制, i 從1開始,循環到9
- 打印??。第 i ?是有 i 項組成的,這 i 項的打印也可以寫成循環來完成。使?循環變量 j , j 從1開始,?致循環到 i ,正好循環 i 次,正好打印 i 項。同時每??打印完后還要換?。
- 輸出的效果中,
i*j
的結果如果是2位數,才有右對?,不夠2位數的情況,使?空格補?。所以這?就得使? %2d 這樣的輸出格式控制了。
#include <iostream>
using namespace std;
#include <cstdio>int main()
{for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", j, i, j * i);}cout << endl;}return 0;
}
練習
乘法表
#include <iostream>
using namespace std;
#include <cstdio>int main()
{for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", j, i, j * i);}cout << endl;}return 0;
}
像題?要求的這種情況,就得使?兩層循環嵌套來解決,外層循環負責控制打印??,內部循環負責控制每??打印?項。
包含數字9的數
#include <iostream>
using namespace std;int main()
{int cnt = 0;for (int i = 1; i <= 2019; i++){int n = i;while (n){if (n % 10 == 9){cnt++;break;}n /= 10;}}cout << cnt << endl;return 0;
}
在多層嵌套的循環中也可以使?break,但是要注意,?個break只能跳出??所在的循環,?法?次性跳出所有的循環
B2064 斐波那契數列
#include <iostream>
using namespace std;int n;int main()
{cin >> n;int a = 0;while (n--){cin >> a;int i = 1, j = 1, k = 1;while (a >= 3){k = i + j;i = j;j = k;a--;}cout << k << endl;}return 0;
}
B2079 求出 e 的值
#include <iostream>
#include <cstdio>
using namespace std;int n;int main()
{cin >> n;double e = 1;for (int j = 1; j <= n; j++){long long x = 1;for (int i = 1; i <= j; i++){x *= i;}e += 1.0 / x;}printf("%.10f", e);return 0;
}
#include <iostream>
using namespace std;
int main()
{ int n = 0; cin >> n; int i = 1; double e = 1; long long fac = 1; while (i <= n) { fac *= i; e += 1.0 / fac; i++; } printf("%.10lf\n", e); return 0;
}
三角形
#include <iostream>
using namespace std;int n;int main()
{cin >> n;for (int i = 1; i <= n; i++){for (int j = 1; j <= i; j++){cout << "*";}cout << endl;}return 0;
}
B2083 畫矩形
#include <iostream>
using namespace std;int a, b, f;
char c;int main()
{cin >> a >> b >> c >> f;if (f == 0){for (int i = 1; i <= a; i++){for (int j = 1; j <= b; j++){if (i == 1 || i == a || j == 1 || j == b)cout << c;elsecout << " "; }cout << endl;}}else{for (int i = 1; i <= a; i++){for (int j = 1; j <= b; j++){cout << c; }cout << endl;}}return 0;
}
如果我們仔細去琢磨上?的代碼,會發現 if 和 else 中都是打印圖案,區別在于實?還是空?,實?和空?的區別?在于中間區域,其實邊上的?圈實?和空?是?樣的。所以我們在實現的時候,邊上?圈打印字符,剩余的區域,做?個判斷,如果是實?打印c,如果是空?就打印空格就好了,那么就有了下?簡化的寫法。
#include <iostream>
using namespace std;int a, b, f;
char c;int main()
{cin >> a >> b >> c >> f;for (int i = 1; i <= a; i++){for (int j = 1; j <= b; j++){if (i == 1 || i == a || j == 1 || j == b)cout << c;else{if (f == 0)cout << " ";elsecout << c; }}cout << endl;}return 0;
}
B2085 第 n 小的質數
解析:
- 質數?稱素數。?個?于1的?然數,除了1和它自身外,不能被其他?然數整除的數叫做質數。
- 第n?的質數,其實就是從?到?的第n個質數。
偽代碼
int i = 2;
int cnt = 0;
while (1)
{判斷i是否是素數試除:拿2~i-1之間的數組去試除i如果2-i-1之間有數字能整除i,則i不是素數如果2-i-1之間沒有任何一個數字能整除i,則i是素數如果i是素數,cnt++if (cnt == n)break;i++;
}
循環停止的時候,i就是第n個素數
#include <iostream>
using namespace std;int n;int main()
{cin >> n;int i = 2;int cnt = 0;while (1){int flag = 1;for (int j = 2; j <= i-1; j++){if (i % j == 0){flag = 0;break;}}if (flag == 1)cnt++;if (cnt == n)break;i++;}cout << i << endl;return 0;
}
“Time Limit Exceeded”(TLE,超時)是?個在編程競賽和在線評測平臺(如LeetCode、Codeforces、HackerRank等)中常?的錯誤信息。它意味著程序在執?過程中超過了給定的最?運?時間限制,?未能在規定時間內得出結果。
如果 n 有?個因? a ,那么必然存在另?個因? b ,使得 n = a × b 。如果 a 和 b 都?于 n \sqrt{ n } n?,那么 a×b 將會?于 n ,這與 n=a×b ?盾。因此?少有?個因?不會超過的。
#include <iostream>
using namespace std;
#include <cmath>int n;int main()
{cin >> n;int i = 2;int cnt = 0;while (1){int flag = 1;for (int j = 2; j <= sqrt(i); j++){if (i % j == 0){flag = 0;break;}}if (flag == 1)cnt++;if (cnt == n)break;i++;}cout << i << endl;return 0;
}
水仙花數
#include <iostream>
#include <cmath>
using namespace std;int main()
{for (int i = 100; i <= 999; i++){int sum = 0;int tmp = i;while (tmp){sum += pow(tmp % 10, 3);tmp /= 10;}if (sum == i)cout << i << endl;}return 0;
}
pow函數,可以?計算次?的函數, pow(x, y) 返回的是 x 的 y 次?的值
pow 函數需要?個頭?件 <cmath>