题目:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a[100000] = { 0 }, n; 6 for (int i = 1; i < 100000; i++) 7 { 8 int x = i, sum = i; 9 while (x != 0) 10 { 11 sum += x % 10; 12 x /= 10; 13 } 14 if (a[sum] == 0 || i < a[sum]) a[sum] = i; 15 } 16 int t; 17 cin >> t; 18 while (t--) 19 { 20 int value; 21 cin >> value; 22 cout << a[value] << endl; 23 } 24 return 0; 25 }
核心在于循环的构造,在进行操作时首先构造最小生成元存在数组中,初始赋值为0,保证无最小元时输出