程序流程结构-循环结构案例-猜数字
1.rand()//伪随机
生成1-100的随机数//rand()%100+1
2.真正想要随机数,要添加随机数种子,利用系统时间生成随机数,防止每次随机数都一样。
srand((unsigned int)time(NULL));
注:还要包含系统时间头文件
#include<ctime>
- 程序流程结构-循环结构案例-敲桌子
1.特殊数字:
7的倍数:a%7==0
个位有7:a%10=7
十位有7:a/10=7(int a)
2.| | //或者符号
- 程序流程结构-嵌套循环
1.利用嵌套循环打印星图
#include<iostream>
using namespace std;
int main() {
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
输出结果
口诀:外层执行一次,内层执行一周。
- 程序流程结构-嵌套循环案例-乘法口诀表
1.列数<=行数 计算结果=列数*行数 根据这个为思路写乘法口诀表
#include<iostream>
using namespace std;
int main() {
int i, j;
for (i = 1; i < 10; i++) {
for (j = 1; j <= i; j++) {
cout << j << '*' << i << '=' << (j * i) << " ";
}
cout << endl;
}
return 0;
}
输出结果:
- 程序流程结构-跳转语句-break语句
1.出现在switch句中
2.出现在循坏语句中
#include<iostream>
using namespace std;
int main(){
int i;
for (i = 0; i <= 10; i++) {
cout << i << endl;
if (i >=5) {
break;
}
}
return 0;
}
结果:
3.出现在嵌套循坏语句中
#include<iostream>
using namespace std;
int main() {
int i, j;
for (i = 0; i <= 10; i++) {
for (j = 0; j <= 10; j++) {
if (j >= 5) {
break;///退出内层循坏!!!!!!
}
cout << "* ";
}
cout << endl;
}
return 0;
}
输出结果: