您的位置:首页 > 其它

UVA - 10161 Ant on a Chessboard

2016-07-14 15:39 204 查看
题目大意:给一个棋盘,输入n,找出n在第几行第几列。

解题思路:找规律。先求最近的完全平方数,得到相应坐标,求 n 与该平方数的差得到 n 的坐标。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main() {
long long  N;
while(scanf("%lld", &N) != EOF && N) {
int x = 0, y = 0;
long long n = floor(sqrt(N) + 0.5);
if (n % 2 == 0) {
x = n;
y = 1;
if (N < n * n)
y = y + n * n - N;
else if (N > n * n) {
x++;
y = N - n * n;
}
}
else {
y = n;
x = 1;
if (N < n * n)
x = x + n * n - N;
else if (N > n * n) {
y++;
x = N - n * n;
}
}
printf("%d %d\n", x, y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva