NO.18十六届蓝桥杯备战|循环嵌套|乘法表|斐波那契|质数|水仙花数|(C++)

[复制链接]
发表于 4 天前 | 显示全部楼层 |阅读模式
循环嵌套

循环嵌套的使⽤

while , do while , for ,这三种循环通常会嵌套在⼀起才气更好的办理题目,就是我们所说的:循环嵌套。这三种循环都可以恣意嵌套使⽤
⽐如
写⼀个代码,打印⼀个乘法⼝诀表
  1. 1*1= 1  
  2. 1*2= 2 2*2= 4  
  3. 1*3= 3 2*3= 6 3*3= 9  
  4. 1*4= 4 2*4= 8 3*4=12 4*4=16
  5. 1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25  
  6. 1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36  
  7. 1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49  
  8. 1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64  
  9. 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 如许的输特别式控制了。
  1. #include <iostream>
  2. using namespace std;
  3. #include <cstdio>
  4. int main()
  5. {
  6.         for (int i = 1; i <= 9; i++)
  7.         {
  8.                 for (int j = 1; j <= i; j++)
  9.                 {
  10.                         printf("%d*%d=%2d ", j, i, j * i);
  11.                 }
  12.                 cout << endl;
  13.         }
  14.         return 0;
  15. }
复制代码
练习

乘法表

  1. #include <iostream>
  2. using namespace std;
  3. #include <cstdio>
  4. int main()
  5. {
  6.         for (int i = 1; i <= 9; i++)
  7.         {
  8.                 for (int j = 1; j <= i; j++)
  9.                 {
  10.                         printf("%d*%d=%2d ", j, i, j * i);
  11.                 }
  12.                 cout << endl;
  13.         }
  14.         return 0;
  15. }
复制代码
像题⽬要求的这种环境,就得使⽤两层循环嵌套来办理,外层循环负责控制打印⼏⾏,内部循环负责控制每⼀⾏打印⼏项。
包罗数字9的数

  1. #include <iostream>
  2. using namespace std;
  3.   
  4. int main()
  5. {
  6.     int cnt = 0;
  7.     for (int i = 1; i <= 2019; i++)
  8.     {
  9.         int n = i;
  10.         while (n)
  11.         {
  12.             if (n % 10 == 9)
  13.             {
  14.                 cnt++;
  15.                 break;
  16.             }
  17.             n /= 10;
  18.         }
  19.     }
  20.     cout << cnt << endl;
  21.     return 0;
  22. }
复制代码
在多层嵌套的循环中也可以使⽤break,但是要注意,⼀个break只能跳出⾃⼰地点的循环,⽆法⼀次性跳出全部的循环
B2064 斐波那契数列

  1. #include <iostream>
  2. using namespace std;
  3. int n;
  4. int main()
  5. {
  6.     cin >> n;
  7.     int a = 0;
  8.     while (n--)
  9.     {
  10.         cin >> a;
  11.         int i = 1, j = 1, k = 1;
  12.         while (a >= 3)
  13.         {
  14.             k = i + j;
  15.             i = j;
  16.             j = k;
  17.             a--;
  18.         }
  19.         cout << k << endl;
  20.     }
  21.     return 0;
  22. }
复制代码
B2079 求出 e 的值

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int n;
  5. int main()
  6. {
  7.     cin >> n;
  8.     double e = 1;
  9.     for (int j = 1; j <= n; j++)
  10.     {
  11.         long long x = 1;
  12.         for (int i = 1; i <= j; i++)
  13.         {
  14.             x *= i;
  15.         }
  16.         e += 1.0 / x;
  17.     }
  18.     printf("%.10f", e);
  19.    
  20.     return 0;
  21. }
复制代码
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.         int n = 0;  
  6.         cin >> n;  
  7.         int i = 1;  
  8.         double e = 1;  
  9.         long long fac = 1;  
  10.        
  11.         while (i <= n)  
  12.         {  
  13.                 fac *= i;  
  14.                 e += 1.0 / fac;  
  15.                 i++;  
  16.         }  
  17.         printf("%.10lf\n", e);  
  18.        
  19.         return 0;  
  20. }
复制代码
三角形

  1. #include <iostream>
  2. using namespace std;
  3. int n;
  4. int main()
  5. {
  6.   cin >> n;
  7.   for (int i = 1; i <= n; i++)
  8.   {
  9.     for (int j = 1; j <= i; j++)
  10.     {
  11.       cout << "*";
  12.     }
  13.     cout << endl;
  14.   }
  15.   return 0;
  16. }
复制代码
B2083 画矩形


  1. #include <iostream>
  2. using namespace std;
  3. int a, b, f;
  4. char c;
  5. int main()
  6. {
  7.     cin >> a >> b >> c >> f;
  8.     if (f == 0)
  9.     {
  10.         for (int i = 1; i <= a; i++)
  11.         {
  12.             for (int j = 1; j <= b; j++)
  13.             {
  14.                 if (i == 1 || i == a || j == 1 || j == b)
  15.                     cout << c;
  16.                 else
  17.                     cout << " ";        
  18.             }
  19.             cout << endl;
  20.         }
  21.     }
  22.     else
  23.     {
  24.         for (int i = 1; i <= a; i++)
  25.         {
  26.             for (int j = 1; j <= b; j++)
  27.             {
  28.                 cout << c;        
  29.             }
  30.             cout << endl;
  31.         }
  32.     }
  33.    
  34.     return 0;
  35. }
复制代码
如果我们过细去琢磨上⾯的代码,会发现 if 和 else 中都是打印图案,区别在于实⼼还是空⼼,实⼼和空⼼的区别⼜在于中心地区,着实边上的⼀圈实⼼和空⼼是⼀样的。以是我们在实现的时间,边上⼀圈打印字符,剩余的地区,做⼀个判断,如果是实⼼打印c,如果是空⼼就打印空格就好了,那么就有了下⾯简化的写法。
  1. #include <iostream>
  2. using namespace std;
  3. int a, b, f;
  4. char c;
  5. int main()
  6. {
  7.     cin >> a >> b >> c >> f;
  8.         for (int i = 1; i <= a; i++)
  9.         {
  10.                 for (int j = 1; j <= b; j++)
  11.                 {
  12.                         if (i == 1 || i == a || j == 1 || j == b)
  13.                                 cout << c;
  14.                         else
  15.                         {
  16.                                 if (f == 0)
  17.                                         cout << " ";
  18.                                 else
  19.                                         cout << c;   
  20.                         }
  21.                                      
  22.                 }
  23.                 cout << endl;
  24.         }
  25.     return 0;
  26. }
复制代码
B2085 第 n 小的质数

剖析:

  • 质数⼜称素数。⼀个⼤于1的⾃然数,除了1和它自身外,不能被其他⾃然数整除的数叫做质数。
  • 第n⼩的质数,着实就是从⼩到⼤的第n个质数。
伪代码
  1. int i = 2;
  2. int cnt = 0;
  3. while (1)
  4. {
  5.         判断i是否是素数
  6.         试除:拿2~i-1之间的数组去试除i
  7.         如果2-i-1之间有数字能整除i,则i不是素数
  8.         如果2-i-1之间没有任何一个数字能整除i,则i是素数
  9.         如果i是素数,cnt++
  10.         if (cnt == n)
  11.                 break;
  12.         i++;
  13. }
  14. 循环停止的时候,i就是第n个素数
复制代码
  1. #include <iostream>
  2. using namespace std;
  3. int n;
  4. int main()
  5. {
  6.     cin >> n;
  7.     int i = 2;
  8.     int cnt = 0;
  9.     while (1)
  10.     {
  11.         int flag = 1;
  12.         for (int j = 2; j <= i-1; j++)
  13.         {
  14.             if (i % j == 0)
  15.             {
  16.                 flag = 0;
  17.                 break;
  18.             }
  19.         }
  20.         if (flag == 1)
  21.             cnt++;
  22.         if (cnt == n)
  23.             break;
  24.         i++;
  25.     }
  26.     cout << i << endl;
  27.    
  28.     return 0;
  29. }
复制代码

“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 ⽭盾。因此⾄少有⼀个因⼦不会凌驾的。
  1. #include <iostream>
  2. using namespace std;
  3. #include <cmath>
  4. int n;
  5. int main()
  6. {
  7.     cin >> n;
  8.     int i = 2;
  9.     int cnt = 0;
  10.     while (1)
  11.     {
  12.         int flag = 1;
  13.         for (int j = 2; j <= sqrt(i); j++)
  14.         {
  15.             if (i % j == 0)
  16.             {
  17.                 flag = 0;
  18.                 break;
  19.             }
  20.         }
  21.         if (flag == 1)
  22.             cnt++;
  23.         if (cnt == n)
  24.             break;
  25.         i++;
  26.     }
  27.     cout << i << endl;
  28.    
  29.     return 0;
  30. }
复制代码
水仙花数

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main()
  5. {
  6.   for (int i = 100; i <= 999; i++)
  7.   {
  8.     int sum = 0;
  9.     int tmp = i;
  10.     while (tmp)
  11.     {
  12.       sum += pow(tmp % 10, 3);
  13.       tmp /= 10;
  14.     }
  15.     if (sum == i)
  16.       cout << i << endl;
  17.   }
  18.   return 0;
  19. }
复制代码
pow函数,可以⽤盘算次⽅的函数, pow(x, y) 返回的是 x 的 y 次⽅的值
pow 函数必要⼀个头⽂件 <cmath>

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表