数字反转之-三位数
#include <iostream> using namespace std; int main(void) { int n; cin >> n; while (n != 0) { cout << n % 10; n /= 10; } return 0; }
吐槽下,输出为正整数021,正整数不应该是21吗。。。。。
如果为正整数21,我会这么写:
#include <iostream> using namespace std; int main(void) { int n; int reverse = 0; cin >> n; while (n != 0) { reverse = reverse * 10 + n % 10; n /= 10; } cout << reverse << endl; return 0; }