现在的位置: 首页 > 综合 > 正文

uvaoj-10161-Ant on a Chessboard

2018年05月04日 ⁄ 综合 ⁄ 共 578字 ⁄ 字号 评论关闭

很简单的找规律的题目,原本我上交的代码WA了,原因是没考虑到 mid=(high+low)/2; 有缺陷,

后来改成 mid=low + (high-low)/2;  就AC了

/*
    uva: 10161
    PROB: Problem A.Ant on a Chessboard
*/
#include<stdio.h>
#define MAXN 10005
#define LOCAL
int main()
{
    #ifdef LOCAL
    freopen("10161.in.txt", "r", stdin);
    #endif // LOCAL
    int n;
    int i, count, low, high, mid, x, y;
    while( scanf("%d", &n) && n )
    {
        for(i=1; ; i++)
            if(i*i>=n)
                break;
        high=i*i;
        low=(i-1)*(i-1)+1;
        mid=low + (high-low)/2;
        if(i%2)
        {
            x=i; y=1;
            count=low;
            while(count<n)
            {
                if(count<mid)
                {
                    y++;  count++;
                }
                else
                {
                    x--;  count++;
                }
            }
        }
        else
        {
            x=i; y=1;
            count=high;
            while(count>n)
            {
                if(count>mid)
                {
                    y++;  count--;
                }
                else
                {
                    x--;  count--;
                }
            }
        }
        printf("%d %d\n", x, y);
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.