Files
python/TangDou/LuoGuBook/P1307_Study.cpp

37 lines
871 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
//反转 办法:模10除10
/*
1234 ---> 1234 % 10 = 4
1234 / 10 =123 123 %10 = 3
123 / 10 =12 12 % 10 = 2
12/10= 1 1 % 10 = 1
123040 -> 40321
*/
using namespace std;
int n; //整数变量n
int main() {
//读入变量n
cin >> n; //从键盘读入
// 0 如果是0,那么输出0
if (n == 0)
cout << 0 << endl;
else {
//如果小于0要输出一个负号 -1234 ---> -4321
if (n < 0) {
cout << "-";
n = -n;
}
bool flag = false; //定义成还没有遇到第一个非零数字
while (n) {
if (n % 10 == 0) {
if (flag) cout << 0;
} else {
flag = true;
cout << n % 10;
}
n /= 10;
}
}
return 0;
}