【2021-02-02】程序流程结构
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //选择结构 单行if语句 6 7 //用户输入分数,如果分数大于600,视为考上一本大学 8 9 //1.用户输入分数 10 int score = 0; 11 cout << "请输入一个分数:" <<endl; 12 cin >> score; 13 //2.打印用户输入的分数 14 cout << "你输入的分数是: " << score << endl; 15 //3.判断分数是否大于600,如果大于等于则输出考上,小于则输出未考上 16 if(score>=600){ 17 cout << "恭喜你考上一本大学!" << endl; 18 } 19 else{ 20 cout << "很可惜你没考上一本" << endl; 21 } 22 //注意:if后不加分号,否则条件无用 23 24 system("pause"); 25 26 return 0; 27 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //多条件if语句 6 //输入一个分数大于600,一本 7 //大于500,二本 8 //大于400,三本 9 //小于等于400,未考上 10 11 int score = 0; 12 cout << "请输入你的考试分数" <<endl; 13 cin >> score; 14 cout << "你输入的分数为:" << score << endl; 15 if(score>600){ 16 cout << "恭喜你考上一本!" << endl; 17 } 18 else if(score>500){ 19 cout << "恭喜你考上二本!" << endl; 20 } 21 else if(score>400){ 22 cout << "恭喜你考上三本!" << endl; 23 } 24 else{ 25 cout << "很可惜,你未考上大学!" << endl; 26 } 27 28 system("pause"); 29 30 return 0; 31 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 /* 5 提示用户输入一个高考考试分数,根据分数做如下判断 6 分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科; 7 在一本分数中,如果大于700分,考入北大,大于650分,考入清华,大于600考入人大。 8 */ 9 int score = 0; 10 cout << "请输入你的考试分数" <<endl; 11 cin >> score; 12 cout << "你输入的分数为:" << score << endl; 13 if(score>600){ 14 cout << "恭喜你考上一本!" << endl; 15 if(score>700){ 16 cout << "恭喜你成功被北京大学录取!" << endl; 17 } 18 else if(score>650) { 19 cout << "恭喜你成功被清华大学录取!" << endl; 20 } 21 else{ 22 cout << "恭喜你成功被人民大学录取!" << endl; 23 } 24 } 25 else if(score>500){ 26 cout << "恭喜你考上二本!" << endl; 27 } 28 else if(score>400){ 29 cout << "恭喜你考上三本!" << endl; 30 } 31 else{ 32 cout << "很可惜,你未考上大学!" << endl; 33 } 34 system("pause"); 35 36 return 0; 37 }
1 #include <iostream> 2 using namespace std; 3 int main(){ 4 //三目运算符 5 //创建三个变量 abc 6 //将a和b进行比较,将变量大的赋值给c 7 8 int a = 10; 9 int b = 20; 10 int c = 0; 11 12 c = (a>b?:b); 13 14 cout << "c = " << c <<endl; 15 16 //在C++中,三目运算符返回的是变量,可以继续赋值 17 (a>b?a:b) = 100; 18 cout << "a = " << a <<endl; 19 cout << "b = " << b <<endl; 20 system("pause"); 21 22 return 0; 23 }
2021-02-03更新
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //switch语句 6 //给电影打分 7 //10-9经典 8 //8-7非常好 9 //6-5一般 10 //5以下烂片 11 12 //给用户提示打分 13 cout<< "请为电影评分!" << endl; 14 int score = 0; 15 cin >> score; 16 cout << "您输入的分数为:" << score << endl; 17 //根据输入分数进行评判 18 switch(score){ 19 case 10: 20 cout << "您认为是经典电影!" << endl; 21 break; 22 case 9 : 23 cout << "您认为是经典电影!" << endl; 24 break; 25 case 8 : 26 cout << "您认为电影非常好!" << endl; 27 break; 28 case 7 : 29 cout << "您认为是电影非常好!" << endl; 30 break; 31 case 6 : 32 cout << "您认为电影一般!" << endl; 33 break; 34 case 5 : 35 cout << "您认为电影一般!" << endl; 36 break; 37 default : 38 cout << "您认为是烂片!" << endl; 39 } 40 41 //缺点 判断的时候只能是整数或者是字符型,不可以是一个区间 42 //优点 结构清晰,执行效率高 43 44 system("pause"); 45 46 return 0; 47 }
1 #include<iostream> 2 #include<stdlib.h> 3 #include<ctime> 4 using namespace std; 5 int main(){ 6 srand((unsigned int)time(NULL)); 7 //添加随机数种子 8 9 10 //1.系统生成随机数 11 int num = rand() % 100 + 1; //rand()%100 生成0-99随机数 12 //cout << num << endl; 13 14 //2.玩家进行猜测 15 int val = 0; 16 cout << "请输入一个1-100以内的数字开始游戏!" << endl; 17 while(1){ 18 cin >> val; 19 if(val>num){ 20 cout << "猜测过大" << endl; 21 } 22 else if(val<num){ 23 cout << "猜测过小" << endl; 24 } 25 else{ 26 cout << "猜对了" << endl; 27 break; 28 } 29 } 30 31 //3.判断玩家猜测 32 33 //猜对 退出游戏 34 //猜错 获得提示 重新返回第二步 35 36 system("pause"); 37 38 return 0; 39 }
1 #include<iostream> 2 #include<stdlib.h> 3 #include<ctime> 4 using namespace std; 5 int main(){ 6 srand((unsigned int)time(NULL)); 7 //添加随机数种子 8 9 10 //1.系统生成随机数 11 int num = rand() % 100 + 1; //rand()%100 生成0-99随机数 12 //cout << num << endl; 13 14 //2.玩家进行猜测 15 int val = 0; 16 cout << "请输入一个1-100以内的数字开始游戏!" << endl; 17 while(1){ 18 cin >> val; 19 if(val>num){ 20 cout << "猜测过大" << endl; 21 } 22 else if(val<num){ 23 cout << "猜测过小" << endl; 24 } 25 else{ 26 cout << "猜对了" << endl; 27 break; 28 } 29 } 30 31 //3.判断玩家猜测 32 33 //猜对 退出游戏 34 //猜错 获得提示 重新返回第二步 35 36 system("pause"); 37 38 return 0; 39 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 //do...while 5 //输出0-9 6 7 int num = 0; 8 9 do{ 10 cout << num << endl; 11 num++; 12 } 13 while(num<10); 14 15 //do while 会先执行一次循环语句 16 17 system("pause"); 18 19 return 0; 20 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //水仙花数 个位^3+十位^3+百位^3=本身 6 //获取个位 153%10=3 7 //获取十位 153/10 = 15 15%10 = 5 8 //获取百位 153/100 = 1 9 10 int num = 100; 11 12 while(num<1000) 13 { 14 int a = 0;//个 15 int b = 0;//十 16 int c = 0;//百 17 a = num%10; 18 b = (num/10)%10; 19 c = num/100; 20 if(a*a*a + b*b*b + c*c*c == num){ 21 cout << num << endl; 22 } 23 num++; 24 } 25 26 system("pause"); 27 28 return 0; 29 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //for循环 6 7 for(int a = 0;a<10;a++){ 8 cout << a << endl; 9 } 10 11 system("pause"); 12 13 return 0; 14 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 //敲桌子 5 //从1开始报数,个位十位含7或者为7的倍数,则打印敲桌子 6 7 for(int i = 1;i<=100;i++){ 8 if((i%7==0)||(i%10==7)||(i/10==7)){ 9 cout << "敲桌子" << endl; 10 } 11 else{ 12 cout << i << endl; 13 } 14 } 15 16 system("pause"); 17 18 return 0; 19 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 //嵌套循环 5 6 for(int j=0;j<10;j++){ 7 for(int i =0;i<10;i++){ 8 cout <<"* " ; 9 } 10 cout << endl; 11 } 12 13 system("pause"); 14 15 return 0; 16 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 //乘法口诀表 5 for(int i = 1;i<10;i++){ 6 for(int j =1;j<=i;j++){ 7 cout << i << "*" << j << "="<< i*j << " " ; 8 } 9 cout << endl; 10 } 11 system("pause"); 12 13 return 0; 14 }

浙公网安备 33010602011771号