ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1079. Maximum

please help me why it is wrong
Posted by xinxin 8 Sep 2016 19:55
#include<stdio.h>
#include<string.h>
int main()
{
    int n,i=0,j,temp=0;
    int a[99999];
    int b[20];
    scanf("%d",&b[0]);
    while(b[i]!=0)
    {
        i++;
        scanf("%d",&b[i]);
    }
    n=i;
    a[0]=0;
    a[1]=1;
    for(i=2;i<99999;i++)
    {
        if(i%2==0)
            a[i]=a[i/2];
        else
            a[i]=a[i/2]+a[i/2+1];
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<=b[i];j++)
        {
            if(a[j]>temp)
            {
                temp=a[j];
            }
        }
        printf("%d\n",temp);
    }
    fflush(stdin);
    getchar();
    return 0;
}
Re: please help me why it is wrong
Posted by Oleg Baskakov 9 Sep 2016 14:10
N can be as high as 99999; however you do int a[99999]; which only creates an array [0..99998].
Sigh, this counterintuitive syntax always creates errors like this.
Re: please help me why it is wrong
Posted by xinxin 9 Sep 2016 19:34
thank you very much
but i change 99999 to 100000,it is still wrong ....sad...why...
Re: please help me why it is wrong
Posted by Oleg Baskakov 9 Sep 2016 19:44
Did you change it from 99999 to 100000 in BOTH places?
Also, you should do temp=0 before your for(j=0;j<=b[i];j++) or otherwise the maximum will remain the same as on previous request (you can try test 10, 5, 0 and it should give you 4 4 with your current code)
Re: please help me why it is wrong
Posted by xinxin 11 Sep 2016 08:08
thank you!!!
it is temp that cause the wrong answer.
thanks!