Regionals 2005 >> Asia-seoul
Problem Link: UVA1583 UVALive3355 Digit Generator. Basic Training level questions, written in C language.
To see test instructions, please click on the link above.
The tabular method is the most reasonable, even if there are too many possibilities, and it is more difficult to find the smallest generator.
The estimated time of not playing the table will also be timed out.
In the program, the table function is encapsulated into the function maketable (), which makes the main function concise. In addition, we need to pay attention to the array subscript overflow problem when playing the table. Also note that the minimum generated element is required.
The following C language programs are adopted by AC:
/* UVA1583 UVALive3355 Digit Generator */#include <stdio.h> #include <memory.h> #define MAXN 100000int ans[ Maxn+1];void maketable (int max) { int i, Digitsum, N; memset (ans, 0, sizeof (ans)); for (I=1; i<max; i++) { digitsum = n = i; while (n > 0) { digitsum + = n; n/=; } if (digitsum <= max) if (ans[digitsum] = = 0) Ans[digitsum] = i; }} int main (void) { maketable (MAXN); int t, N; scanf ("%d", &t); while (t--) { scanf ("%d", &n); printf ("%d\n", Ans[n]); } return 0;}
UVA1583 UVALive3355 Digit Generator