1595: TomCat要吃饭
Time Limit: 1 Sec Memory Limit: 128 MB[ Submit][ Status][ Web Board]
Description
TomCat 是一次单身Cat,但是他有着很多的基友,每次吃饭都会QQ他的某一只基友,然而,他的基友Jerry 总是很墨迹。等待是无聊的,这时TomCat在学校的地上乱走,这时他发现地上有方块铺成的,他按照下图的规律走,现在无聊的TomCat想知道,他如果走了n步,他应该在什么位置,位置用坐标(x,y)表示。
Input
每次输入一个数n,n=0是输入结束(n<=2*10^9)
Output
输出坐标(x,y)
Sample Input
8
20
25
0
Sample Output
2 3
5 4
1 5
【分析】
首先我们可以找到目标n在哪个角.
可以发现,对角线上的位置(1,1),(2,2)...(x,x)的值z=x*(x-1)+1;
然后用n去比较,就可以知道n所在的大概位置,肯定是(x,y)或者(y,x)
这个时候如果去比较,就可以发现,y=x-abs(x-z);
然后就是判断到底是(x,y)还是(y,x)的问题了,判断一下奇偶就可以了~
当然 如果是一个平方数,那么需要特殊判断,平方数肯定在边上也就是肯定是(1,sqrt(n))或(sqrt(n),1)
判断一下sqrt(n)的奇偶就可以了
【代码】
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main()
{
long long x;
while (~scanf("%lld",&x)&& x)
{
long long a=sqrt(x);
if (a*a==x)
{
if (a%2) printf("1 %lld\n",a);
else printf("%lld 1\n",a);
continue;
}
a++;
long long b=(a-1)*a+1;
long long c=a-abs(x-b);
if (a%2)
{
if (x<b) printf("%lld %lld",a,c);
else printf("%lld %lld",c,a);
}
else
{
if (x>b) printf("%lld %lld",a,c);
else printf("%lld %lld",c,a);
}
printf("\n");
}
}