例23.1
- 当n等于6,后天是星期1
- 当n等于7,后天是星期二。
- 其它正常情况,直接输出n+2即可
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
if (n == 6)
cout << 1;
else if (n == 7)
cout << 2;
else
cout << n + 2;
return 0;
}
例23.2
- 当是大月时,直接输出31
- 当是小月时(不包含2月),直接输出30
- 当闰年2月时,输出29,否则输出28
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
cin >> m >> n;
if (n == 1 or n == 3 or n == 5 or n == 7 or n == 8 or n == 10 or n == 12)
cout << 31;
else if (n == 4 or n == 6 or n == 9 or n == 11)
cout << 30;
else if (n == 2 and !(m % 4) and (!(m % 400) or m % 100))
cout << 29;
else
cout << 28;
return 0;
}
例23.3
用if语句也可以,但大家可以尝试一下switch语句(我基本不用switch哈哈)
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
cin>>a>>b>>c;
switch(c){
case '+': cout << a+b << endl; break;
case '-': cout<<a-b<<endl;break;
case '*': cout<<a*b<<endl;break;
case '/':
{
if(b==0){
cout<<"Divided by zero!"<<endl;
}else{
cout<<a/b<<endl;
}
break;
}
default:cout<<"Invalid operator!"<<endl;
}
return 0;
}
练23.1
又是一道很简单的题啦!判断分数范围,输出对应的等级就可以啦!
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
cin >> n;
if (n >= 90)
cout << 'A';
else if (n >= 70)
cout << 'B';
else if (n >= 60)
cout << 'C';
else
cout << 'D';
return 0;
}
例23.2
也很简单,输出对应的菜单就行啦
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
if (n == 1)
cout << "寿仙菇";
else if (n == 2)
cout << "酒糟芋";
else if (n == 3)
cout << "下山笋";
else if (n == 4)
cout << "太师豆腐";
else if (n == 5)
cout << "孝子鱼";
else if (n == 6)
cout << "猪全福";
else
cout << "无此编号的菜";
return 0;
}
练23.3
用if语句会更简单哈!
这里就是判断星期一、三、五无法赴约,其它能赴约
(对了,这里祝各位早日脱单哈!)
(反正我早就脱了)
(三国杀中许褚有一台词:脱!)
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
switch(a)
{
case 1:cout<<"NO"<<endl;break;
case 3:cout<<"NO"<<endl;break;
case 5:cout<<"NO"<<endl;break;
default:cout<<"YES"<<endl;
}
return 0;
}
练23.4
输入字符,输出对应结果就行啦
#include<bits/stdc++.h>
using namespace std;
char c;
int main(){
cin >> c;
if(c == 'a')
cout << "apple";
else if(c == 'b')
cout << "banana";
else if(c == 'c')
cout << "cat";
else
cout << "no";
return 0;
}