Uva1583

Digit Generator UVA - 1583

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 #include <vector>
 5 #include<string.h>
 6 #include<map>
 7 #include<bits/stdc++.h>
 8 #define LL long long
 9 #define maxn 100100
10 using namespace std;
11 int n,ans,a[maxn];//a[原来的数]=最小生成元
12 //这里想一下,如果是a[最小生成元]=原来的数,那么在下面的输入原来的数找最小生成元的时候就需要遍历整个数组了,增加了没必要的复杂度
13 //我们的目标是o(1),通过输入一个数直接得到结果。
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i=1;i<maxn;i++)
18     {//i是最小生成元,现在是知道最小生成元求原来的数
19         int x=i,y=i;//求出的y是原来的数
20         while(x)
21         {
22             y+=x%10;
23             x/=10;
24         }//a[y]=i;
25         if(a[y]==0||a[y]>i)
26             a[y]=i;
27     }
28     while(n--)
29     {
30         scanf("%d",&ans);
31         printf("%d
",a[ans]);
32     }
33     return 0;
34 }

思路:

函数映射:a[原来的数]=最小生成元

注意点:

这里一定要注意maxn的范围,100005会WA。但是不知道为什么,题目给的原来的数的范围是1<=N<=100,000,而最小生成元一定比原来的数要小<=100,000,那么把最小生成元的范围开到100,005应该是足够了,但是WA了。不明白为什么,请大佬们指教了。

原文地址:https://www.cnblogs.com/zuiaimiusi/p/10898636.html