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

news/2025/2/21 13:57:13/

循环嵌套

循环嵌套的使⽤

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
  1. 打印的内容是分为9⾏的,第1⾏打印1项,第2⾏打印2项,第3⾏打印3项,以此类推,第9⾏打印9项。
  2. 每⼀⾏的信息,每⼀项的第⼀个乘数和列号是⼀致的,每⼀项的第⼆个乘数和⾏号是⼀致的,两个乘数的积就是第三个数。
  3. 打印9⾏,我们可以使⽤循环解决,总共循环9次,每次进⼊循环打印⼀⾏,循环变量使⽤ i 来控制, i 从1开始,循环到9
  4. 打印⼀⾏。第 i ⾏是有 i 项组成的,这 i 项的打印也可以写成循环来完成。使⽤循环变量 j , j 从1开始,⼀致循环到 i ,正好循环 i 次,正好打印 i 项。同时每⼀⾏打印完后还要换⾏。
  5. 输出的效果中,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 画矩形

![[Pasted image 20250214192601.png]]

#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的⾃然数,除了1和它自身外,不能被其他⾃然数整除的数叫做质数。
  2. 第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;
}

![[Pasted image 20250214195145.png]]

“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>


http://www.ppmy.cn/news/1573635.html

相关文章

性格测评小程序07用户登录

目录 1 创建登录页2 在首页检查登录状态3 搭建登录功能最终效果总结 小程序注册功能开发好了之后&#xff0c;就需要考虑登录的问题。首先要考虑谁作为首页&#xff0c;如果把登录页作为首页&#xff0c;比较简单&#xff0c;每次访问的时候都需要登录。 如果把功能页作为首页&…

23种设计模式 - 装饰器模式

模式定义 装饰器模式&#xff08;Decorator Pattern&#xff09;是一种结构型设计模式&#xff0c;允许动态地为对象添加新功能&#xff0c;而无需修改其结构。它通过将对象包装在装饰器类的实例中&#xff0c;实现功能的灵活扩展&#xff0c;符合开放/封闭原则。 模式结构 抽…

C++ 设计模式-外观模式

外观模式的定义 外观模式是一种 结构型设计模式,它通过提供一个简化的接口来隐藏系统的复杂性。外观模式的核心思想是: 封装复杂子系统:将多个复杂的子系统或组件封装在一个统一的接口后面。提供简单接口:为客户端提供一个更简单、更易用的接口,而不需要客户端直接与复杂…

Node.js前后端交互核心技术全解

一、HTTP通信交互核心实践 1. URI解析与动态路由 原理&#xff1a;URI是资源定位的关键标识&#xff0c;Node.js通过url和querystring模块解析路径和查询参数。 代码示例&#xff1a; const http require(http); const url require(url);const server http.createServer(…

numpy(02 数据类型和数据类型转换)

numpy(01 入门) 目录 一、Python NumPy 数据类型 1.1 NumPy 基本类型 1.2 数据类型对象 (dtype) 1.3 具体实例 二、Numpy数据类型转换 2.1 浮点数据转换 2.2 整型数据转换 2.3 浮点数转整数 一、Python NumPy 数据类型 1.1 NumPy 基本类型 下表列举了常用 NumPy 基…

使用Druid连接池优化Spring Boot应用中的数据库连接

使用Druid连接池优化Spring Boot应用中的数据库连接 使用Druid连接池优化Spring Boot应用中的数据库连接1. 什么是Druid连接池&#xff1f;2. 在Spring Boot中配置Druid连接池2.1 添加依赖2.2 配置Druid连接池2.3 配置参数详解 3. 启用Druid监控4. 总结 使用Druid连接池优化Spr…

如何通过 Homebrew 安装 Qt 并配置环境变量

如何通过 Homebrew 安装 Qt 并配置环境变量 Qt 是一个跨平台的应用程序开发框架&#xff0c;广泛用于开发图形界面应用。本文将详细介绍如何在 macOS 上通过 Homebrew 安装 Qt 并配置环境变量&#xff0c;以便在终端和 Qt Creator 中使用 Qt 工具。 步骤 1&#xff1a;安装 Ho…

【大模型】DeepSeek 高级提示词技巧使用详解

目录 一、前言 二、DeepSeek 通用提示词技巧 2.1 DeepSeek 通用提示词技巧总结 三、DeepSeek 进阶使用技巧 3.1 DeepSeek一个特定角色的人设 3.1.1 为DeepSeek设置角色操作案例一 3.1.2 为DeepSeek设置角色操作案例二 3.2 DeepSeek开放人设升级 3.2.1 特殊的人设&#…