Algorithmic Race Primer Classic (2nd edition) 3rd Chapter array and string
Example 3-5 generating meta UVa1583
Sentiment.
0, check the code in the book, the IF (ans[y]==0| | M<ans[y]) are suspicious. Start tracking
if (ans[y]==0| | M<ans[y]) {
if (M<ans[y])
printf ("m=%d ans[y]=%d y=%d\n", m,ans[y],y);
Ans[y]=m;
}
Print as follows:
m=99991 ans[y]=3811788 y=100028
m=99993 ans[y]=3811784 y=100032
m=99995 ans[y]=3811792 y=100036
It is clear that the array is out of bounds, the book Ans Array range 0-100004.
The proposed changes are as follows:
if (y<=n&&ans[y]==0) {
Ans[y]=m;
}
1, faint feeling this question, tabulation, check the table the fastest, when not firm enough, glanced at the hint (not see code), and want to agree, began to start programming.
2, very simple a problem, filled with joy to submit, unexpectedly WA, decisively check the original problem in English, the original input and output format is limited.
Sample Input
3
216
121
2005
Sample Output
198
0
1979
3, immediately submit AC, view the code in the book than I notch above, did I want to do not do the corresponding relationship ans[y]=m.
Attached code:
Environmental dev-cpp4.9.9.2
#include <stdio.h>
const int MAXN=100000+10;
int A[MAXN];
int bsum (int b) {//Gets the sum of the numbers in each position of the integer
int sum=0;
while (b) {
sum+=b%10;
b/=10;
}
return sum;
}
int main () {
int i;
int n;
int T;
for (i=1;i<=100000;i++) {//tab
A[i]=bsum (i) +i;
}
scanf ("%d", &t);
while (T) {
t--;
scanf ("%d", &n);
for (i=1;i<n;i++) {//Tabular
if (a[i]==n)
Break
}
if (i==n)//Search No results
printf ("%d\n", 0);
Else
printf ("%d\n", I);
}
return 0;
}