例22.1
注意关键词:只有。说明有且仅有一门科目不及格才算
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if(a>=60 && b<60 || b>=60 && a<60){
cout<<"1"<<endl;
}else{
cout<<"0"<<endl;
}
return 0;
}
例22.2
众所周知,若a mod 5 !=0 或 b mod 5 != 0,则ab mod 5 != 0.
当然,你要算乘积再求余也可以。
那么如果数据非常大怎么办(大到无法相乘,且数据太大求余麻烦)?
其实只要看个位就行啦~我是不是很聪明?
#include<bits/stdc++.h>
using namespace std;
unsigned long long a, b;
int main(){
cin >> a >> b;
if(!(a % 5) or !(b % 5))
cout << "Yes";
else
cout << "No";
return 0;
}
例22.3
1,首先,我们需要读入两个正整数 n 和 m,表示 a 和 b 的值。
2.接下来,我们需要计算 a+b 的结果 sum,并判断 sum 是否是回文数。
3.根据题目描述,回文数是将一个自然数的各位数字反向排列所得到的自然数。例如,121 是回文数。
4.我们可以利用以下思路判断一个数是否是回文数:
首先,声明一个函数 h,该函数的作用是判断一个数是否是回文数。
在函数 h 中,我们将传入的数 n 进行反向排列得到数 temp3。
如果原始数 temp 和反向排列后的数 temp3 相等,则说明该数是回文数,返回 true;否则返回 false。
5.在主函数中,计算 a+b 的结果 sum,并输出 sum。
6.然后,判断 sum 是否是回文数,如果是,输出 “Yes”;否则,输出 “No”。
#include <bits/stdc++.h>
using namespace std;
int n, m, sum; // 声明三个整型变量 n、m 和 sum
bool h(int n) { // 判断一个数是否是回文数的函数
int temp = n, temp3 = 0;
while (n) // 反向排列得到的数
temp3 *= 10, temp3 += n % 10, n /= 10;
if (temp == temp3) // 如果原始数和反向排列后的数相等,则是回文数
return true;
else
return false;
}
int main() {
cin >> n >> m; // 读入 a 和 b 的值
sum = n + m; // 计算 a+b 的结果
cout << sum << '\n'; // 输出 sum
if (h(sum)) // 判断 sum 是否是回文数
cout << "Yes"; // 如果是回文数,输出 "Yes"
else
cout << "No"; // 否则,输出 "No"
return 0;
}
练22.1
根据题目给的条件判断闰年就行喽!
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
if (!(n % 4) and (!(n % 400) or n % 100))
cout << n << "是闰年";
else
cout << n << "是平年";
return 0;
}
练22.2
思路1:单独判断除以3和除以5的结果
思路2:要是3和5的公倍数必须是15的倍数,故判断除以15的余数即可
#include <iostream>
using namespace std;
int n;
int main() {
cin >> n;
if (!(n % 3) and !(n % 5))
cout << "欧耶欧耶";
return 0;
}
练22.3
这里使用字符串解决
意思是:如果千位=十位,且百位=个位,则输出YES
否则输出NO
#include <iostream>
#include <cstring>
using namespace std;
string s;
int main() {
cin >> s;
if (s[0] == s[2] and s[1] == s[3])
cout << "YES";
else
cout << "NO";
return 0;
}
练22.4
判断能否构成三角形:任意两边大于第三边就能构成三角形
#include <iostream>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
if (a + b > c and a + c > b and b + c > a)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}