例21.1
一连串超长判断,其实本质一样,都很简单
#include <bits/stdc++.h>
using namespace std;
long long n;
int main(){
cin >> n;
if(n <= 150)
cout << "S";
else if(n > 150 and n <= 160)
cout << "M";
else if(n > 160 and n <= 170)
cout << "L";
else if(n > 170 and n <= 180)
cout << "XL";
else
cout << "XXL";
return 0;
}
例21.2
这里使用字符串解决,字符串中数字字符个数即是答案
#include <bits/stdc++.h>
using namespace std;
long long n;
string s;
int main(){
cin >> s;
cout << s.size();
return 0;
}
例21.3
判断方法见代码:
值得一提的是,为什么判断字符也能用大于小于等于符号直接能比较?
不要问,问就是不知道
因为它们比较的方法是判断ASCLL码,所以就能用那些符号啦!
#include <bits/stdc++.h>
using namespace std;
long long n;
char c;
int main(){
cin >> c;
if(c >= 'A' and c <= 'Z')//是否大写字符
cout << "upper";
else if(c >= '0' and c <= '9')//是否数字
cout << "digit";
else if(c >= 'a' and c <= 'z')//是否小写字符
cout << "lower";
else//是否其它字符
cout << "other";
return 0;
}
练21.1
又是一连串if判断
#include <bits/stdc++.h>
using namespace std;
long long n;
int main(){
cin >> n;
if(n <= 3)
cout << "infant";
else if(n > 3 and n <= 12)
cout << "child";
else if(n > 13 and n <= 18)
cout << "youngster";
else
cout << "youth";
return 0;
}
练21.2
注意:题目别理解错,每公斤的收费是指对应的范围
#include <bits/stdc++.h>
using namespace std;
long long n;
int main(){
cin >> n; // 读入包裹的重量
if(n > 30) // 如果包裹的重量超过 30 千克,无法邮寄
cout << "Fail";
else if(n <= 10) // 如果包裹的重量小于等于 10 千克
cout << fixed << setprecision(2) << 0.2 + n * 0.8; // 所需费用为 0.2 + n * 0.8
else if(n > 10 and n <= 20) // 如果包裹的重量大于 10 千克且小于等于 20 千克
cout << fixed << setprecision(2) << 8.2 + (n - 10) * 0.75; // 所需费用为 8.2 + (n - 10) * 0.75
else // 如果包裹的重量大于 20 千克且小于等于 30 千克
cout << fixed << setprecision(2) << 15.7 + (n - 20) * 0.7; // 所需费用为 15.7 + (n - 20) * 0.7
return 0;
}
练21.3
判断递增、递减、相等,详细见代码
#include <iostream>
using namespace std;
int a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
if (a < b and b < c and c < d)//判断递增
cout << "Fish Rising";
else if (a > b and b > c and c > d)//判断递减
cout << "Fish Diving";
else if (a == b and b == c and c == d)//判断相等
cout << "Fish At Constant Depth";
else
cout << "No Fish";
return 0;
}
练21.4
根据公式计算体质指数,再根据体质指数判断胖瘦程度
#include<bits/stdc++.h>
using namespace std;
int main(){
double height, weight, BMI;
cin >> height >> weight;
BMI = weight / (height * height);
if (BMI < 18.5)
cout << "偏瘦";
else if (BMI >= 18.5 and BMI <= 23.9)
cout << "正常";
else if (BMI >= 24 and BMI <= 27.9)
cout << "偏胖";
else if (BMI >= 28 and BMI <= 39.9)
cout << "肥胖";
else
cout << "极重度肥胖";
return 0;
}
练21.5
1.首先,我们需要读入两个整数 n 和 m,分别表示行车路程和停车时间。
2.接下来,我们需要根据行车路程和停车时间计算费用。
3.根据题目描述,计价方案如下:
- 2千米以内起步费为6元;
- 超过2千米后按1.8元/千米计价;
- 超过10千米后在1.8元/千米的基础上加价50%;
- 停车等候时间每3分钟加收1元,不满3分钟不计费。
4.我们可以利用以下规律进行计算费用:
- 如果行车路程小于等于2千米,则费用为起步费6元加上停车等候费;
- 如果行车路程大于2千米且小于等于10千米,则费用为起步费6元加上超过2千米的部分按照1.8元/千米计算的费用,再加上停车等候费;
- 如果行车路程大于10千米,则费用为起步费6元加上超过2千米且不超过10千米的部分按照1.8元/千米计算的费用,再加上超过10千米的部分按照2.7元/千米计算的费用,最后再加上停车等候费。
#include <bits/stdc++.h>
using namespace std;
int n, m; // 声明两个整型变量 n 和 m
int main() {
cin >> n >> m; // 读入行车路程和停车时间
m = floor(m / 3.0); // 计算停车等候费
if (n <= 2) // 如果行车路程小于等于2千米
cout << 6 + m; // 费用为起步费6元加上停车等候费
else if (n > 2 and n <= 10) // 如果行车路程大于2千米且小于等于10千米
cout << m + 6 + (n - 2) * 1.8; // 费用为起步费6元加上超过2千米的部分按照1.8元/千米计算的费用,再加上停车等候费
else // 如果行车路程大于10千米
cout << m + 20.4 + (n - 10) * 2.7; // 费用为起步费6元加上超过2千米且不超过10千米的部分按照1.8元/千米计算的费用,再加上超过10千米的部分按照2.7元/千米计算的费用,最后再加上停车等候费
return 0;
}