倒叙输出:
--- 简单版,十位数
该程序用于将一个两位数倒序输出,并处理个位数为0的情况。
#include <iostream>
using namespace std;
int main(){
int x;
cin>>x;
int q = x%10;
int a = x/10%10;
if(q == 0)
{
cout<<a;
}
else
{
cout<<q<<a;
}
return 0;
}
- 输入:
20
,输出:2
- 输入:
45
,输出:54
---复杂版,六位数
该程序用于将一个六位数倒序输出,逐位提取并输出每一位数字。
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x; // 输入一个六位数
int a = x % 100000 % 10000 % 1000 % 100 % 10; // 提取个位数字
int b = x % 100000 % 10000 % 1000 % 100 / 10; // 提取十位数字
int c = x % 100000 % 10000 % 1000 / 100; // 提取百位数字
int d = x % 100000 % 10000 / 1000; // 提取千位数字
int e = x % 100000 / 10000; // 提取万位数字
int f = x / 100000; // 提取十万位数字
cout << f << endl << e << endl << d << endl << c << endl << b << endl << a; // 输出各位数字
return 0;
}
(这个拆分过程可能写的不是最简便的。。。)
输入:123456
输出:
6
5
4
3
2
1
最大最小值相减:
该程序用于将一个三位数拆分为百位、十位和个位数字,找出其中的最大值和最小值,并计算两者之间的差值。
#include <iostream>
using namespace std;
int main() {
int n, b, s, g, m, x; // b, s, g 分别代表百位、十位、个位
cin >> n;
// 对 n 进行拆位
b = n / 100;
s = n / 10 % 10;
g = n % 10;
// 找出最大值
if (b > s && b > g) {
m = b;
} else if (s > g) {
m = s;
} else {
m = g;
}
// 找出最小值
if (b < s && b < g) {
x = b;
} else if (s < g) {
x = s;
} else {
x = g;
}
// 计算并输出差值
cout << m - x;
return 0;
}
- 输入:
352
,输出:3
(因为最大值为5,最小值为2,差值为3)
判断闰年
该程序用于判断输入的年份是否为闰年。
闰年的判断规则是:能被4整除但不能被100整除,或者能被400整除。
#include <iostream>
using namespace std;
int main(){
int x;
cin>>x;
if(x%4==0&&x%100!=0||(x%400==0)){
cout<<"yes";
}
else{
cout<<"no";
}
return 0;
}
- 输入:
2000
,输出:yes
- 输入:
1900
,输出:no
判断数字性质
该程序用于判断输入的数字为正数还是负数, 偶数还是奇数。
#include <iostream>
using namespace std;
int main() {
int num;
cout << "请输入一个整数: ";
cin >> num;
if (num == 0) {
cout << "这是零" << endl;
} else {
if (num > 0) {
cout << "这是正数" << endl;
} else {
cout << "这是负数" << endl;
}
if (num % 2 == 0) {
cout << "这是偶数" << endl;
} else {
cout << "这是奇数" << endl;
}
}
return 0;
}
输入:8,
输出:正数
奇数
//以上题均为我做分支题常用较难的代码,适用于不同场景。有什么不足之处,欢迎指正!