循环语句
循环语句
for 语句、while 语句、do while 语句;多层循环语句。
概念
循环语句让一段代码按条件重复执行:for 适合已知次数或带明确初值/步长;while 先判条件再执行;do while 至少执行一次再判条件。多层循环是外层每走一轮,内层完整跑一遍——复杂度与下标范围由此决定。与 cspj.cpp.branch 不同:循环解决「重复」,分支解决「择一」。与 cspj.algo.enumerate-simulate 不同:后者是算法思想,这里是语言结构本身。CSP-J 阅读程序爱考 for 里 i++/++i、边界 < n 还是 <= n;完善程序爱考循环变量初值、步长、终止条件三要素是否配套。
CSP-J 常考
cspj-r1-2024第 7、20、33 题:单选/阅读/完善程序,考查循环次数、break/continue或嵌套循环变量变化。cspj-r1-2025第 16、35、37 题:阅读与完善程序,跟踪for/while累加或下标更新。cspj-r1-2020第 18–19、34、36 题:阅读判断循环是否终止、完善程序补循环上界或初值。cspj-r1-2019第 4、18 题:单选与阅读,问循环执行次数或变量最终值。
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i)
sum += i;
cout << sum << endl; // 1+2+...+n
易错点
for (int i = 0; i < n; i++)与i <= n-1等价,但与i <= n差一次迭代,完善程序空位常在这里设陷阱。- 阅读程序里在循环体中修改循环变量(如
i--)或内层改用外层变量,要手工模拟前几轮,不要凭感觉猜「大概跑 n 次」。 do while(0)等写法偶尔出现,条件为假时也已执行过一轮 body。
若有如下程序段,其中 s、a、b、c 均已定义为整型变量,且 a、c 均已赋值(c 大于 0)
s = a;
for (b = 1; b <= c; b++) s = s - 1;
则与上述程序段功能等价的赋值语句是()
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
原卷作答说明:判断题正确填 √,错误填 ×
#include <cstdio>2#include <cstring>3using namespace std;4char st[100];5int main() {6 scanf("%s", st);7 int n = strlen(st);8 for (int i = 1; i <= n; ++i) {9 if (n % i == 0) {10 char c = st[i - 1];11 if (c >= 'a')12 st[i - 1] = c - 'a' + 'A';13 }14 }15 printf("%s", st);16 return 0;17}- 18判断1.5 分
若将第 8 行的
i <= n改为i * i <= n,程序运行结果不会改变。()
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
原卷作答说明:判断题正确填 √,错误填 ×
#include <cstdlib>2#include <iostream>3using namespace std;45char encoder[26] = {'C', 'S', 'P', 0};6char decoder[26];78string st;910int main() {11 int k = 0;12 for (int i = 0; i < 26; ++i)13 if (encoder[i] != 0) ++k;14 for (char x = 'A'; x <= 'Z'; ++x) {15 bool flag = true;16 for (int i = 0; i < 26; ++i)17 if (encoder[i] == x) {18 flag = false;19 break;20 }21 if (flag) {22 encoder[k] = x;23 ++k;24 }25 }26 for (int i = 0; i < 26; ++i)27 decoder[encoder[i] - 'A'] = i + 'A';28 cin >> st;29 for (int i = 0; i < st.length(); ++i)30 st[i] = decoder[st[i] - 'A'];31 cout << st;32 return 0;33}- 18判断1.5 分
将第 12 行的
i < 26改为i < 16,程序运行结果不会改变。( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
原卷作答说明:判断题正确填 √,错误填 ×
#include <cstdlib>2#include <iostream>3using namespace std;45char encoder[26] = {'C', 'S', 'P', 0};6char decoder[26];78string st;910int main() {11 int k = 0;12 for (int i = 0; i < 26; ++i)13 if (encoder[i] != 0) ++k;14 for (char x = 'A'; x <= 'Z'; ++x) {15 bool flag = true;16 for (int i = 0; i < 26; ++i)17 if (encoder[i] == x) {18 flag = false;19 break;20 }21 if (flag) {22 encoder[k] = x;23 ++k;24 }25 }26 for (int i = 0; i < 26; ++i)27 decoder[encoder[i] - 'A'] = i + 'A';28 cin >> st;29 for (int i = 0; i < st.length(); ++i)30 st[i] = decoder[st[i] - 'A'];31 cout << st;32 return 0;33}- 19判断1.5 分
将第 26 行的
i < 26改为i < 16,程序运行结果不会改变。( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
三、完善程序(单选题,每小题 3 分,共计 30 分)
1.(质因数分解)给出正整数 n,请输出将 n 质因数分解的结果,结果从小到大输出。
例如:输入 n=120,程序应该输出 2 2 2 3 5,表示:120=2×2×2×3×5。输入保证 2≤n≤109。
提示:先从小到大枚举变量 i,然后用 i 不停试除 n 来寻找所有的质因子。
试补全程序。
#include <cstdio>
using namespace std;
int n, i;
int main() {
scanf("%d", &n);
for(i = ①; ② <=n; i ++){
③{
printf("%d ", i);
n = n / i;
}
}
if(④)
printf("%d ", ⑤);
return 0;
}
#include <cstdio>2using namespace std;3int n, i;45int main() {6 scanf("%d", &n);7 for(i = ①; ② <=n; i ++){8 ③{9 printf("%d ", i);10 n = n / i;11 }12 }13 if(④)14 printf("%d ", ⑤);15 return 0;16}- 34(①)单选空位 ①3 分
①处应填( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
三、完善程序(单选题,每小题 3 分,共计 30 分)
1.(质因数分解)给出正整数 n,请输出将 n 质因数分解的结果,结果从小到大输出。
例如:输入 n=120,程序应该输出 2 2 2 3 5,表示:120=2×2×2×3×5。输入保证 2≤n≤109。
提示:先从小到大枚举变量 i,然后用 i 不停试除 n 来寻找所有的质因子。
试补全程序。
#include <cstdio>
using namespace std;
int n, i;
int main() {
scanf("%d", &n);
for(i = ①; ② <=n; i ++){
③{
printf("%d ", i);
n = n / i;
}
}
if(④)
printf("%d ", ⑤);
return 0;
}
#include <cstdio>2using namespace std;3int n, i;45int main() {6 scanf("%d", &n);7 for(i = ①; ② <=n; i ++){8 ③{9 printf("%d ", i);10 n = n / i;11 }12 }13 if(④)14 printf("%d ", ⑤);15 return 0;16}- 36(③)单选空位 ③3 分
③处应填( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
原卷作答说明:判断题正确填 √,错误填 ×
#include <iostream>2using namespace std;34int n;5int a[1000];67int f(int x)8{9 int ret = 0;10 for (; x; x &= x - 1) ret++;11 return ret;12}1314int g(int x)15{16 return x & -x;17}1819int main()20{21 cin >> n;22 for (int i = 0; i < n; i++) cin >> a[i];23 for (int i = 0; i < n; i++)24 cout << f(a[i]) + g(a[i]) << ' ';25 cout << endl;26 return 0;27}- 17判断1.5 分
输入的 a[i] 必须全为正整数,否则程序将陷入死循环。( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
(1)(Josephus 问题) 有 n 个人围成一个圈,依次标号 0 至 n−1。从 0 号开始,依次 0,1,0,1,… 交替报数,报到 1 的人会离开,直至圈中只剩下一个人。求最后剩下人的编号。
试补全模拟程序。
#include <iostream>23using namespace std;45const int MAXN = 1000000;6int F[MAXN];78int main() {9 int n;10 cin >> n;11 int i = 0, p = 0, c = 0;12 while (①) {13 if (F[i] == 0) {14 if (②) {15 F[i] = 1;16 ③;17 }18 ④;19 }20 ⑤;21 }22 int ans = -1;23 for (i = 0; i < n; i++)24 if (F[i] == 0)25 ans = i;26 cout << ans << endl;27 return 0;28}- 38(⑤)单选空位 ⑤3 分
⑤处应填( )
A.i++
B.i = (i + 1) % n
C.c++
D.p ^= 1
以下哪个不是 C++ 中的循环语句?( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
#include <iostream>2using namespace std;34bool isPrime(int n) {5 if (n <= 1) {6 return false;7 }8 for (int i = 2; i * i <= n; i++) {9 if (n % i == 0) {10 return false;11 }12 }13 return true;14}1516int countPrimes(int n) {17 int count = 0;18 for (int i = 2; i <= n; i++) {19 if (isPrime(i)) {20 count++;21 }22 }23 return count;24}2526int sumPrimes(int n) {27 int sum = 0;28 for (int i = 2; i <= n; i++) {29 if (isPrime(i)) {30 sum += i;31 }32 }33 return sum;34}3536int main() {37 int x;38 cin >> x;39 cout << countPrimes(x) << " " << sumPrimes(x) << endl;40 return 0;41}- 20单选3 分
如果将
for (int i = 2; i * i <= n; i++)改为for (int i = 2; i <= n; i++),输入 10 时,程序的输出( )。
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
第 1 题
(判断平方数) 问题:给定一个正整数 n,希望判断这个数是否为完全平方数,即存在一个正整数 x,使得 x 的平方为 n。
试补全程序。
#include<iostream>
#include<vector>
using namespace std;
bool isSquare(int num) {
int i = ___①___;
int bound = ___②___;
for (; i <= bound; ++i) {
if (___③___) {
return ___④___;
}
}
return___⑤___;
}
int main() {
int n;
cin >> n;
if (isSquare(n)) {
cout << n << " is a square number" << endl;
} else {
cout << n << " is not a square number" << endl;
}
return 0;
}
#include<iostream>2#include<vector>3using namespace std;45bool isSquare(int num) {6 int i = ___①___;7 int bound = ___②___;8 for (; i <= bound; ++i) {9 if (___③___) {10 return ___④___;11 }12 }13 return___⑤___;14}1516int main() {17 int n;18 cin >> n;19 if (isSquare(n)) {20 cout << n << " is a square number" << endl;21 } else {22 cout << n << " is not a square number" << endl;23 }24 return 0;25}- 33(①)单选空位 ①3 分
① 处应填( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
原卷作答说明:判断题正确填 A,错误填 B
#include <algorithm>2#include <cstdio>3#include <cstring>4inline int gcd(int a, int b) {5 if (b == 0)6 return a;7 return gcd(b, a % b);8}9int main() {10 int n;11 scanf("%d", &n);12 int ans = 0;13 for (int i = 1; i <= n; ++i) {14 for (int j = i + 1; j <= n; ++j) {15 for (int k = j + 1; k <= n; ++k) {16 if (gcd(i, j) == 1 && gcd(j, k) == 117 && gcd(i, k) == 1) {18 ++ans;19 }20 }21 }22 }23 printf("%d\n", ans);24 return 0;25}- 16判断1 分
(1 分)当输入为 2 时,程序并不会执行第 16 行的判断语句。( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
(1)(字符串解码)“行程长度编码”(Run-Length Encoding)是一种无损压缩算法,常用于压缩重复字符较多的数据,以减少存储空间。假设原始字符串不包含数字字符。压缩规则如下:i) 如果原始字符串中一个字符连续出现 N 次(N≥2),在压缩字符串中它被表示为“字符 + 数字 N”。例如,编码 A12 代表 12 个连续的字符 A。ii) 如果原始字符串中一个字符只出现 1 次,在压缩字符串中它就表示为该字符本身。例如,编码 B 代表 1 个字符 B。
以下程序实现读取压缩字符串并输出其原始的、解压后的形式。试补全程序。
#include <cctype>2#include <iostream>3#include <string>4using namespace std;56int main() {7 string z;8 cin >> z;9 string s = "";1011 for (int i = 0; i < z.length(); ) {12 char ch = z[i];1314 if (__①__ && isdigit(z[i + 1])) {15 i++;16 int count = 0;17 while (i < z.length() && isdigit(z[i])) {18 count = __②__;19 i++;20 }21 for (int j = 0; j < __③__; ++j) {22 s += ch;23 }24 } else {25 s += __④__;26 __⑤__;27 }28 }2930 cout << s << endl;31 return 0;32}- 35(③)单选空位 ③3 分
③处应填( )
整组作答后提交批改,成绩计入掌握度 · 已答 0/1
(1)(字符串解码)“行程长度编码”(Run-Length Encoding)是一种无损压缩算法,常用于压缩重复字符较多的数据,以减少存储空间。假设原始字符串不包含数字字符。压缩规则如下:i) 如果原始字符串中一个字符连续出现 N 次(N≥2),在压缩字符串中它被表示为“字符 + 数字 N”。例如,编码 A12 代表 12 个连续的字符 A。ii) 如果原始字符串中一个字符只出现 1 次,在压缩字符串中它就表示为该字符本身。例如,编码 B 代表 1 个字符 B。
以下程序实现读取压缩字符串并输出其原始的、解压后的形式。试补全程序。
#include <cctype>2#include <iostream>3#include <string>4using namespace std;56int main() {7 string z;8 cin >> z;9 string s = "";1011 for (int i = 0; i < z.length(); ) {12 char ch = z[i];1314 if (__①__ && isdigit(z[i + 1])) {15 i++;16 int count = 0;17 while (i < z.length() && isdigit(z[i])) {18 count = __②__;19 i++;20 }21 for (int j = 0; j < __③__; ++j) {22 s += ch;23 }24 } else {25 s += __④__;26 __⑤__;27 }28 }2930 cout << s << endl;31 return 0;32}- 37(⑤)单选空位 ⑤3 分
⑤处应填( )