C05-3.16分支结构3
原题链接:38471.note12025-03-16 18:15:27
发布于:江苏
一、switch开关变量的类型
#include<iostream>
using namespace std;
int main()
{
char n; //注意这里的类型是 char
cin >> n;
switch(n){
case '1': cout << 1;
case '2': cout << 2;
case '3': cout << 3; break;
default : cout << "input error";
}
return 0;
}
homework
#include <iostream>
using namespace std;
int main()
{
double n, money = 0;
cin >> n;
if (n <= 15)
money = n * 0.47;
else if (n <= 30)
money = 15*0.47 + (n-15)*0.89;
else
money = 15*0.47 + 15*0.89 + (n-30)*1;
printf("%.2lf", money);
return 0;
}
二、逻辑运算符
逻辑运算符: 逻辑与(&&), 逻辑或(||), 逻辑非(!)
优先级顺序 : ! > && > ||
实例 优秀学生2
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n>=60 && n<=80 && n%2==0) {
cout << "yes";
}
else {
cout << "no";
}
return 0;
}
实例 优秀学生3
#include<iostream>
using namespace std;
int main(){
int chinese, math;
cin >> chinese >> math ;
if(chinese>=90&&math>=90||chinese+math>=185) {
cout << "yes";
} else {
cout << "no";
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a = 23, b = 13, c = 0;
cout << ((a>b) || (a < b)) << endl; //1
cout << ((a<c) && (a>b) || (a<c)) << endl; //0
cout << ((a<c) || (a>b) && (a>c)) << endl; //1
cout << (!0) << endl;
return 0;
}
实例,闰年判断
年份:
世纪年份 y%100 == 0
普通年份 y%100 != 0
闰年
世纪闰年: y%400==0
普通闰年: y%100!=0 && y%4==0
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n ;
if(n % 100 != 0 && n % 4 == 0 || n % 400 == 0){
cout << "KeYi";
}else {
cout << "BuKeYi";
}
return 0;
}
这里空空如也
有帮助,赞一个