Example 3-5 generating meta UVa1583

Source: Internet
Author: User
Tags printf
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.