循环不需要写到9999,因为i*9要小于等于9999,所以循环到1111即可。
暴力解:
#include <iostream> using namespace std; int main() { int a, b, c, d; int temp; int aa,bb,cc,dd; for (int i = 1000; i < 1111; i++){ temp = i; d = temp%10; temp /= 10; c = temp%10; temp /= 10; b = temp%10; temp /= 10; a = temp%10; temp = i*9; dd = temp%10; temp /= 10; cc = temp%10; temp /= 10; bb = temp%10; temp /= 10; aa = temp%10; // cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl; // cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<endl; if (d == aa && c == bb && b == cc && a == dd) cout << i << endl; } } // 64 位输出请用 printf("%lld")
暴力解 简洁代码
#include <iostream> using namespace std; int main() { int a, b, c, d; int temp; int aa,bb,cc,dd; for (int i = 1000; i < 1111; i++){ if ((i%10) == (i*9/1000)%10 && (i/10)%10 == (i*9/100)%10 && (i/100)%10 == (i*9/10)%10 && (i/1000)%10 == (i*9)%10) cout << i << endl; } }