- level 1:语法基础课
第 3 讲 循环结构
- 2023-8-2 16:13:08 @
学习编程语言语法是次要的,思维是主要的。如何把头脑中的想法变成简洁的代码,至关重要。
学习循环语句只需要抓住一点——代码执行顺序!
一、while
循环
可以简单理解为循环版的if
语句。if
语句是判断一次,如果条件成立,则执行后面的语句;while
是每次判断,如果成立,则执行循环体中的语句,否则停止。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i = 0;
if (i < 10)
{
cout << i << endl;
i ++ ;
}
while (i < 10)
{
cout << i << endl;
i ++ ;
}
return 0;
}
练习:求 1~100 中所有数的立方和。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i = 1, sum = 0;
while (i <= 100)
{
sum += i * i * i;
i ++ ;
}
cout << sum << endl;
return 0;
}
练习:求斐波那契数列的第n
项。f(1) = 1
, f(2) = 1
, f(3) = 2
, f(n) = f(n-1) + f(n-2)
。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a = 1, b = 1, i = 1;
while (i < n)
{
int c = a + b;
a = b;
b = c;
i ++ ;
}
cout << a << endl;
return 0;
}
死循环:循环永久执行,无法结束。我们要避免写出死循环。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 1;
while (x == 1) puts("!");
return 0;
}
二、do while
循环
do while
循环不常用。
do while
语句与while
语句非常相似。唯一的区别是,do while
语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 1;
while (x < 1)
{
cout << "x!" << endl;
x ++ ;
}
int y = 1;
do // 循环体只有一句时可省略大括号
{
cout << "y!" << endl;
} while (y < 1);
return 0;
}
三、for
循环
基本思想:把控制循环次数的变量从循环体中剥离。
for (init-statement ; condition ; expression)
{
statement
}
init-statement
可以是声明语句、表达式、空语句,一般用来初始化循环变量;
condition
是条件表达式,和while
中的条件表达式作用一样;可以为空,空语句表示true
;
expression
一般负责修改循环变量,可以为空。
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int i = 0; i < 10; i ++ )
{
cout << i << endl;
}
return 0;
}
练习:求 1~100 中所有数的立方和。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int s = 0;
for (int i = 1; i <= 100; i ++ )
{
s += i * i * i;
}
cout << s << endl;
return 0;
}
练习:求斐波那契数列的第n
项。f(1) = 1
, f(2) = 1
, f(3) = 2
, f(n) = f(n-1) + f(n-2)
。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 1, b = 1;
int n;
cin >> n;
for (int i = 0; i < n - 1; i ++ )
{
int c = a + b;
a = b;
b = c;
}
cout << a << endl;
return 0;
}
init-statement
可以定义多个变量,expression
也可以修改多个变量。
例如求 1 * 10 + 2 * 9 + 3 * 8 + 4 * 7 + 5 * 6
:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int sum = 0;
for (int i = 1, j = 10; i < j; i ++, j -- )
{
sum += i * j;
}
cout << sum << endl;
return 0;
}
四、跳转语句
- break
可以提前从循环中退出,一般与if语句搭配。
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i ++ )
{
cout << i << endl;
if (i == 49) break;
}
return 0;
}
例题:判断一个大于1的数是否是质数:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
bool is_prime = true;
for (int i = 2; i < n; i ++ )
if (n % i == 0)
{
is_prime = false;
break;
}
if (is_prime) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
- continue
可以直接跳到当前循环体的结尾。作用与if语句类似。
例题:求1~100中所有偶数的和。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int sum = 0;
for (int i = 1; i <= 100; i ++ )
{
if (i % 2 == 1) continue;
sum += i;
}
cout << sum << endl;
return 0;
}
五、多层循环
英语需要背因为设计语言的时候有很多特例,编程语言由数学家设计很精妙只记规则
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int i = 0, k = 1; i < 10; i ++ )
{
for (int j = 0; j < 10; j ++, k ++ )
{
cout << k << ' '; // printf("%-5d ", k);
}
cout << endl;
}
return 0;
}
练习:打印九九乘法表
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int i = 1; i <= 9; i ++ )
{
for (int j = 1; j <= i; j ++ )
cout << i << '*' << j << '=' << setw(2) << i * j << ' ';
cout << endl;
}
return 0;
}
练习:打印1~100中的所有质数
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int i = 2; i <= 100; i ++ )
{
bool is_prime = true;
for (int j = 2; j < i; j ++ )
{
if (i % j == 0)
{
is_prime = false;
break;
}
}
if (is_prime) cout << i << endl;
}
return 0;
}
练习:输入一个n,打印n阶菱形。n是奇数。
n = 9时的结果:
*
***
*****
*******
*********
*******
*****
***
*
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int cx = n / 2, cy = n / 2;
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ )
if (abs(i - cx) + abs(j - cy) <= n / 2)
cout << '*';
else
cout << ' ';
cout << endl;
}
return 0;
}
备注:
- 读入的个数未知时,可以用
while(cin >> x)
或while(scanf("%d", &x) != -1)
或while(~scanf("%d", &x))
来输入。如果输入的最后一个为 0 且该 0 不处理,输入语句可以用while(cin >> x && x)
或while(cin >> x, x)
,逗号表达式取最后一个值。 EOF(end of file) 键盘输入EOF文件结束符:win ctrl + z linux ctrl + d cin 是有返回值的 cout << (cin >> x) << endl; - DEV 编译选项里加-std=c++14
- 一般来讲由于递归不存储中间结果,所以递归的效率应该是比循环低,但程序员效率高
- 时间复杂度,TLE体验
立方和公式: 公式推导 杨辉三角https://www.bilibili.com/video/BV1P741117QQ/?spm\_id\_from=333.999.0.0
设1^3+2^3+…n^3=P(n)两边取导数得
3(1^2+2^2+…+n^2)=P(n)的导数
由于1^2+2^2+…+n^2=1/6n(n+1)(2n+1)
所以P(n)的导数=1/2n(n+1)(2n+1)=1/2(2n^3+3n^2+n)
再对1/2(2n^3+3n^2+n)取积分得1/4(n^4+2n^3+n^2)+C(C为常数)
化简得((1+n)n/2)^2+C
将n=1代入 由((1+n)n/2)^2+C=1得C=0
所以P(n)=((1+n)n/2)^2
1 comments
-
夜雨声烦 LV 6 @ 2024-10-23 19:18:27
c++光速通关教程—— 退竞赛
🤡 10👍 9👀 7🤣 6🌿 5🕊️ 5😕 5🍋 5👎 2
- 1