Fibsieve had a fantabulous (yes, it's an actual word) birthday party this year. He had so many gifts that he was actually thinking of not having a party next year. Among these gifts there was an N x N glass chessboard that had a light in each of its cells. When the board was turned on a distinct cell would light up every second, and then go dark.
The cells would light up in the sequence shown in the diagram. Each cell is marked with the second in which it would light up.
25 | 24 | 23 | 22 | 21 |
10 | 11 | 12 | 13 | 20 |
9 | 8 | 7 | 14 | 19 |
2 | 3 | 6 | 15 | 18 |
1 | 4 | 5 | 16 | 17 |
In the first second the light at cell (1, 1) would be on. And in the 5th second the cell (3, 1) would be on. Now, Fibsieve is trying to predict which cell will light up at a certain time (given in seconds). Assume that N is large enough.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case will contain an integer S (1 ≤ S ≤ 1015) which stands for the time.
Output
For each case you have to print the case number and two numbers (x, y), the column and the row number.
Sample
Inputcopy | Outputcopy |
---|---|
3 8 20 25 | Case 1: 2 3 Case 2: 5 4 Case 3: 1 5 |
题目大意:
给定一个N * N 数组(有序如图)
输入一个数,输出改数字的坐标(从1号开始为原点建立坐标系);
优先考虑找规律
先写一个暴力代码
如下
#include<bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define int i64
#define endl '\n'
int n,m,sum,ans,A,B;
int a[6][6] =
{
{0,0,0,0,0,0},
{0,25,24,23,22,21},
{0,10,11,12,13,20},
{0,9,8,7,14,19},
{0,2,3,6,15,18},
{0,1,4,5,16,17},
};
void solve()
{
for(int i = 1;i <= 25;i++)
{
for(int j = 5;j >= 1;j--)
for(int k = 1;k <= 5;k++)
if(a[j][k] == i)
{
cout << i << " : " << k << " " << 5 - j + 1 << endl;
}
}
}
signed main()
{
cin.tie(0) -> sync_with_stdio(false);
int T = 1;
//cin >> T;
while(T--)
solve();
return 0;
}
得到结果:
/*
1 : 1 1
2 : 1 2
3 : 2 2
4 : 2 1
5 : 3 1
6 : 3 2
7 : 3 3
8 : 2 3
9 : 1 3
10 : 1 4
11 : 2 4
12 : 3 4
13 : 4 4
14 : 4 3
15 : 4 2
16 : 4 1
17 : 5 1
18 : 5 2
19 : 5 3
20 : 5 4
21 : 5 5
22 : 4 5
23 : 3 5
24 : 2 5
25 : 1 5
*/
AC代码:
#include<bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define int i64
#define endl '\n'
int n,m,sum,ans,A,B;
int a[6][6] =
{
{0,0,0,0,0,0},
{0,25,24,23,22,21},
{0,10,11,12,13,20},
{0,9,8,7,14,19},
{0,2,3,6,15,18},
{0,1,4,5,16,17},
};
/* 列 行
3 1 : 2 2
5 2 : 3 1
7 2 : 3 3
2 1 : 1 2
6 2 : 3 2
8 2 : 2 3
10 3 : 1 4
*/
void solve()
{
// for(int i = 1;i <= 25;i++)
// {
// for(int j = 5;j >= 1;j--)
// for(int k = 1;k <= 5;k++)
// if(a[j][k] == i)
// {
// cout << i << " " << int(sqrt(i)) << " : " << k << " " << 5 - j + 1 << endl;
// }
// }
int t,k;
cin >> t;
for(int i = 1;i <= t;i++)
{
cin >> k;
int q = (int)sqrt(double(k));
if(q * q == k)
k & 1 ? (n = 1, m = q) : (n = q, m = 1);
else
{
int c = q * (q + 1) + 1;
if(q & 1)
k < c ? (n = (q + 1) - (c - k),m = q + 1) : (n = q + 1,m = (q + 1) - (k - c));
else
k < c ? (n = q + 1,m = (q + 1) - (c - k)) : (n = (q + 1) - (k - c),m = q + 1);
}
cout << "Case "<< i << ": " << n << " " << m << endl;
}
}
signed main()
{
cin.tie(0) -> sync_with_stdio(false);
int T = 1;
//cin >> T;
while(T--)
solve();
return 0;
}
剔除测试代码
#include<bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define int i64
#define endl '\n'
int n,m,sum,ans,A,B;
void solve()
{
int t,k;
cin >> t;
for(int i = 1;i <= t;i++)
{
cin >> k;
int q = (int)sqrt(double(k));
if(q * q == k)
k & 1 ? (n = 1, m = q) : (n = q, m = 1);
else
{
int c = q * (q + 1) + 1;
if(q & 1)
k < c ? (n = (q + 1) - (c - k),m = q + 1) : (n = q + 1,m = (q + 1) - (k - c));
else
k < c ? (n = q + 1,m = (q + 1) - (c - k)) : (n = (q + 1) - (k - c),m = q + 1);
}
cout << "Case "<< i << ": " << n << " " << m << endl;
}
}
signed main()
{
cin.tie(0) -> sync_with_stdio(false);
int T = 1;
while(T--)
solve();
return 0;
}