常用判断条件
基本比较
// 等于(注意与赋值=的区别)
if (a == b) // 等于
if (a != b) // 不等于
if (a > b) // 大于
if (a >= b) // 大于等于
if (a < b) // 小于
if (a <= b) // 小于等于
范围判断
// 判断在区间内
if (x >= 0 && x <= 100) // [0, 100]闭区间
if (x > 0 && x < 100) // (0, 100)开区间
if (score >= 60 && score < 90) // [60, 90)
// 判断在区间外
if (x < 0 || x > 100) // (-∞, 0) ∪ (100, +∞)
if (!(x >= 0 && x <= 100)) // 等价于上一行
// 多个条件组合
if ((age >= 18 && age <= 60) && (height >= 160))
取余运算应用
if (n % 2 == 0) // 偶数
if (n % 2 != 0) // 奇数
if (n % 2 == 1) // 正奇数(负数情况要注意)
位运算应用(高阶用法、仅了解)
if ((n & 1) == 0) // 偶数(二进制末位为0)
if (n & 1) // 奇数(二进制末位为1)
整除判断
if (n % 3 == 0) // 能被3整除
if (n % 5 != 0) // 不能被5整除
if (n % 2 == 0 && n % 3 == 0) // 同时被2和3整除(6的倍数)
if (n % 4 == 0 || n % 6 == 0) // 能被4或6整除
特殊数值判断
// 零值判断
if (x == 0)
if (x != 0)
// 正负判断
if (x > 0) // 正数
if (x < 0) // 负数
if (x >= 0) // 非负数
if (x <= 0) // 非正数
// 特定值判断
if (x == 100) // 等于100
if (x != -1) // 不等于-1(常用于结束标志)
if (abs(x) == 1) // 绝对值为1 abs()是自带绝对值函数
字符判断
#include <cctype> // 包含字符判断函数
// 大小写字母
if (ch >= 'A' && ch <= 'Z') // 大写字母
if (ch >= 'a' && ch <= 'z') // 小写字母
if (isupper(ch)) // 标准库函数判断大写
if (islower(ch)) // 标准库函数判断小写
// 数字字符
if (ch >= '0' && ch <= '9') // 数字字符
if (isdigit(ch)) // 标准库函数判断数字
// 其他字符
if (ch == ' ' || ch == '\t' || ch == '\n') // 空白字符
if (isspace(ch)) // 标准库函数判断空白
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') // 运算符
与或非运算
与运算(&&) - 所有条件都要满足
if (age >= 18 && score >= 60) // 成年且及格
if (a > 0 && b > 0 && c > 0) // 三个数都为正
或运算(||) - 至少一个条件满足
if (score < 60 || score > 90) // 不及格或优秀
if (ch == 'Y' || ch == 'y') // 大小写Y都接受
非运算(!) - 条件取反
if (!(score >= 60)) // 等价于 score < 60
if (!isFinished) // 未完成
混合使用注意优先级
// && 优先级高于 ||
if (age >= 18 && (gender == 'M' || gender == 'F')) // 正确
if (age >= 18 && gender == 'M' || gender == 'F') // 可能错误
// 括号明确优先级
if ((score >= 90 && attendance > 0.8) || isSpecial)
三角形构成条件
if (a + b > c && a + c > b && b + c > a) // 能构成三角形
闰年判断
if ((year%4==0 && year%100!=0) || (year%400==0))
三数排序/比较
// 找最大值
if (a >= b && a >= c) max = a;
else if (b >= a && b >= c) max = b;
else max = c;
// 判断是否升序
if (a <= b && b <= c) // 升序排列
相等判断技巧
// 三数相等
if (a == b && b == c)
// 两数相等,第三数不同
if (a == b && b != c)
// 至少有两个数相等
if (a == b || b == c || a == c)
赋值与比较
if (x = 5) // 错误!这是赋值,此时总是为真,因为5就是true
if (x == 5) // 正确!比较
浮点数比较
double a = 0.1 + 0.2;
if (a == 0.3) // 错误!浮点数精度问题
if (abs(a - 0.3) < 1e-6) // 正确!允许微小误差
交换变量的值
使用中间变量(最常用)
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
// 使用临时变量交换
int temp = a;
a = b;
b = temp;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
使用加减法(无临时变量)
a = a + b; // a现在保存和
b = a - b; // b得到原来的a值
a = a - b; // a得到原来的b值
使用异或运算(无临时变量)
a = a ^ b; // a = a XOR b
b = a ^ b; // b = (a XOR b) XOR b = a
a = a ^ b; // a = (a XOR b) XOR a = b
使用标准库函数 swap(最推荐)
#include <iostream>
#include <utility> // 或 <algorithm>(旧版本)
using namespace std;
int main() {
int x = 5, y = 7;
swap(x, y); // 推荐使用
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
数据拆分与回文数
固定长度数字拆分(已知位数)
// 方法1:数学计算法
int num = 123;
int hundred = num / 100; // 百位:123/100 = 1
int ten = num / 10 % 10; // 十位:123/10=12, 12%10=2
int unit = num % 10; // 个位:123%10 = 3
// 方法2:直接取余法
hundred = num / 100; // 百位
ten = num % 100 / 10; // 十位:123%100=23, 23/10=2
unit = num % 10; // 个位
非固定长度数字拆分(未知位数)
正向拆分(从个位到最高位)
int num = 12345;
int digit;
// 边拆分边处理
while (num > 0) {
digit = num % 10; // 取最后一位
cout << digit << " "; // 输出:5 4 3 2 1
num /= 10; // 去掉最后一位
}
反向拆分(从最高位到个位)
int num = 12345;
int temp = num;
int divisor = 1;
// 先找到最高位的权值
while (temp >= 10) {
divisor *= 10; // 最终divisor=10000
temp /= 10;
}
// 从最高位开始拆分
temp = num;
while (divisor > 0) {
int digit = temp / divisor; // 取当前最高位
cout << digit << " "; // 输出:1 2 3 4 5
temp %= divisor; // 去掉最高位
divisor /= 10; // 权值减少10倍
}
回文数判断
回文数指正反读一样的数字,如12321、1221、5、0
反转数字法(最常用)
int original = num; // 备份
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10; // 反转拼接
num /= 10;
}
if (reversed == original) {
cout << "yes";
}
else {
cout << "no";
}
累加累乘问题
固定解题步骤:1、确定循环范围;2、确定初始值;3、确定累加或累乘项
求结果
#include <iostream>
using namespace std;
int main(){
int n,s=0; // 累乘s=1
cin>>n;
for(int i=1;i<=n;i++){
s=s+i; // 累乘s=s*i
}
cout<<s;
return 0;
}
求上限的项
#include <iostream>
using namespace std;
int main(){
int n=1,s=0,k; // 累乘s=1
cin>>k;
while(true){
s=s+n; // 累乘s=s+n
if(s>k) break;
n++;
}
cout<<n;
return 0;
}
求最值问题
原则:求最大设最小、求最小设最大
#include <iostream>
#include <climits>
using namespace std;
int main(){
int n,a;
cin>>n;
int maxa=INT_MIN,mina=INT_MAX;
for(int i=1;i<=n;i++){
cin>>a;
if(maxa<a){
maxa=a;
}
if(mina>a){
mina=a;
}
}
cout<<maxa<<" "<<mina;
return 0;
}