Pages

Wednesday 3 October 2012

UVA - 10161 - Ant on a Chessboard

#include <stdio.h>
#include <math.h>


int main()
{
        long x;
        while(1){
            scanf("%ld",&x);
            if(x==0)
                break;
            int sqr=ceil(sqrt(x));
            long i,j;
            if(sqr%2==0){
                long temp=sqr*sqr-sqr+1;
                i=sqr;
                j=sqr;
                while(temp<x){
                    j--;
                    temp++;
                }
                while(temp>x){
                    i--;
                    temp--;
                }
            }
            else{
                long temp=sqr*sqr-sqr+1;
                i=sqr;
                j=sqr;
                while(temp<x){
                    i--;
                    temp++;

                }
                while(temp>x){
                    j--;
                    temp--;
                }
            }
            printf("%ld %ld\n",i,j);
        }
    return 0;
}

No comments:

Post a Comment