循环嵌套
循环嵌套:循环里面还有循环
小口诀 : 外层循环负责打印多少行; 内层循环负责具体的某一行是什么样
#include <iostream>
using namespace std;
// 小口诀 : 外层循环负责打印多少行; 内层循环负责具体的某一行是什么样
int main(){
// 外层循环负责打印 10 行
for(int i=0; i < 10; i++){
// 内层循环每行打印 5个 *
for(int j=0; j < 5; j++){
cout<< "*"<<" ";
}
// 换行就行
cout<<endl;
}
}
自己实现九九乘法表
#include <iostream>
using namespace std;
// 小口诀 : 外层循环负责打印多少行; 内层循环负责具体的某一行是什么样
int main(){
for(int i = 1; i < 10; i++){
for(int j = 1; j <= i; j++){
cout<< j << " x " << i << " = " << i*j<< " ";
}
cout<<endl;
}
}
#include <iostream>
using namespace std;
// 小口诀 : 外层循环负责打印多少行; 内层循环负责具体的某一行是什么样
int main(){
int row = 10;
int column;
while(row>=1){
column = 1;
while(column<=10){
// 打印
// if(row%2==0)
// cout<< ">";
// else
// cout<< "<";
cout<< (row%2? "<" : ">");
++column;
}
cout<<endl;
--row;
}
}
for 形式
#include <iostream>
using namespace std;
int main(){
for (int row = 10; row >= 1 ; --row) {
for (int column = 1; column <=10 ; ++column)
cout<< (row%2? "<" : ">");
cout<<endl;
}
}
输入一个数 判断他是不是水仙花数
#include <iostream>
using namespace std;
int main(){
int num;
cout<< "input a number";
cin>>num;
// 提取个位十位百位
int ge, shi, bai;
ge = num%10;
bai = num/100;
shi = (num%100-ge)/10;
if ((ge*ge*ge + shi*shi*shi + bai*bai*bai) == num)
cout<< "yes"<<endl;
else
cout<< "no"<<endl;
}
寻找所有的水仙花数
#include <iostream>
#include<cmath>
using namespace std;
int main(){
int num = 101;
while(num<1000){
// 提取个位十位百位
int ge, shi, bai;
ge = num%10;
bai = num/100;
shi = (num%100-ge)/10;
if ((pow(ge, 3) + pow(shi, 3) + pow(bai, 3)) == num)
cout<<num <<" ";
num++;
}
}
goto语句
作用 可以无条件的跳转语句
#include <iostream>
#include<cmath>
using namespace std;
int main(){
cout<< "1" <<endl;
goto FLAG;
cout<< "2" <<endl;
cout<< "3" <<endl;
cout<< "4" <<endl;
FLAG:
cout<< "5" <<endl;
}
注意:在程序中不建议使用goto语句,以免造成程序流程混乱
数组
所谓数组,就是⼀个集合,⾥⾯存放了相同类型的数据元素
特点1:数组中的每个数据元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的
⼀维数组
定义方式(三种):
1. 数据类型 数组名[数组长度];
2. 数据类型 数组名[数组长度] = {值1, 值2, ...}
3. 数据类型 数组名[] = {值1, 值2, ...}
#include <iostream>
#include<cmath>
using namespace std;
int main(){
// 数据类型 数组名[元素个数] 像变量的先声明没赋值
int score[10];
// 利用数组下标进行赋值 下标(索引) 是从零开始的
score[0] = 100;
score[1] = 99;
score[2] = 88;
// 利用下标访问数组
cout<< score[0] <<endl;
cout<< score[1] <<endl;
cout<< score[2] <<endl;
cout<< score[3] <<endl; // -1516864905 没有进行初始化的数组是一个不确定的值
cout<<"+++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
// 数据类型 数组名[元素个数] = {值1,值2, 值3.. } 像变量的声明枚举赋值
// 如果{}内不足10个数据,剩余数据用0补全
int score2[10] = {99, 22, 55, 88};
cout<< score2[0] <<endl;
cout<< score2[1] <<endl;
cout<< score2[4] <<endl;
cout<< score2[5] <<endl;
// 数据类型 数组名[] = {值1,值2, 值3.. }
int score3[] = {99, 22, 55, 66, 77, 99};
cout<<"+++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
// 将数组中的所有元素取出, 我们一般叫遍历
for(int i = 0; i < 6; i++)
cout<< score3[i]<< " ";
}
⼀维数组数组名
⼀维数组名称的⽤途:
- 可以统计整个数组在内存中的⻓度
- 可以获取数组在内存中的⾸地址
#include <iostream>
#include<cmath>
using namespace std;
int main(){
// 可以获取数组在内存中的⾸地址
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout<<arr<<endl; // 0x61fee8
// 可以统计整个数组在内存中的⻓度(元素个数)
cout<< "all arr size = " << sizeof(arr) << endl;
cout<< "one arr size = " << sizeof(arr[0]) << endl;
cout<< "arr length = " << sizeof(arr)/sizeof(arr[0]) << endl;
for(int i =0; i< sizeof(arr)/sizeof(arr[0]); i++)
cout<< arr[i]<< " " ;
}