题解 | #反序数#
反序数
https://www.nowcoder.com/practice/e0d06e79efa44785be5b2ec6e66ba898
#include<vector> #include<string> #include<iostream> using namespace std; int main() { int reverse_n; for (int i = 1000; i < 1111; i++) { reverse_n = i * 9; if (reverse_n/1000==i%10) { if ((reverse_n/100)%10==(i%100)/10) { if ((reverse_n%100)/10== (i % 100) / 100) { if (reverse_n%10==i/1000) { cout << i << endl; } } } } } }
本题关键在于:如何Reverse
Question:求一个数x的反序数
int Reverse(int x) { int revx = 0; while (x != 0) { revx *= 10; revx += x % 10; x /= 10; } return x; }
改进后的代码
#include<vector> #include<string> #include<iostream> using namespace std; int Reverse(int x) { int revx = 0; while (x != 0) { revx *= 10; revx += x % 10; x /= 10; } return revx; } int main() { int reverse_n; for (int i = 1000; i < 1111; i++) { reverse_n = i * 9; int reverse_i = Reverse(i); if (reverse_i==reverse_n) { cout << i << endl; } } }