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 1209. 1, 10, 100, 1000...

my program,why wa?please :(
Posted by Yinthewind 28 Apr 2011 09:04
#include<stdio.h>
long long a[65537];
int bsearch(int b,int t,long long k)
{
    int mid=(b+t)/2;
    if(b>t)
    {
        return 0;
    }
    if(k>a[mid])
    {
        bsearch(mid+1,t,k);
    }
    else if(k<a[mid])
    {
        bsearch(b,mid-1,k);
    }
    else return 1;
}
void main()
{
    int n;
    long long k,i;
    scanf("%d",&n);
    for(i=1;i<=65536;i++)
    {
        a[i]=i*(i-1)/2+1;
    }
    while(n>0)
    {
        scanf("%lld",&k);
        printf("%d",bsearch(1,65536,k));
        if(n!=1)
            printf(" ");
        n--;
    }
}
Re: my program,why wa?please :(
Posted by Ibragim Atadjanov (Tashkent U of IT) 28 Apr 2011 20:07
I don't know your mistake. But I can tell you that you can find out if the given k is really 1 without binary search. It's just a simple math. It's a formula and you know the formula.
k = x (x - 1) / 2 + 1 just find x and make sure that it has an positive integer value. good luck
Re: my program,why wa?please :(
Posted by Yinthewind 29 Apr 2011 05:13
thanks