ZCMU 1595: TomCat要吃饭
Time Limit: 1 Sec Memory Limit: 128 MB
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
思路
水~~~~汤姆又皮了
找规律如下图
没找过最优解法,手动发现横行底行偶数位和竖行左行奇数位值都是位置的平方,于是暴力找出比题目提问大的平方值,其中一个坐标即为平方值的根,另外一个坐标为平方值与提问值的差值加一,第一个点套进公式算不出,于是分开讨论。具体哪个是x哪个是y,用if分类讨论。话不投机上代码......
AC代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <list>
#include <algorithm>
#include <set>
#include <list>
using namespace std;
int n;
int main()
{
while(true)
{
scanf("%d", &n);
if (!n)return 0;
if (n == 1) { printf("1 1\n"); continue; }
int i, res, flag = false;
for (i = 1, res = 1; res < n; i++)
{
res = i * i;
}
i--;
int x, y;
if (i%2)
{
if(n>=res-i+1)
{
y = i;
x = res - n + 1;
}
else if(n<res-i+1)
{
x = i;
y = n - (res - 2 * i + 1);
}
}
else
{
if (n >= res - i + 1)
{
x = i;
y = res - n + 1;
}
else if (n<res - i + 1)
{
y = i;
x = n - (res - 2 * i + 1);
}
}
printf("%d %d\n", x, y);
}
}