7-1 Square Friends (20 分)
For any given positive integer n, two positive integers A and B are called Square Friends if by attaching 3 digits to every one of the n consecutive numbers starting from A, we can obtain the squares of the n consecutive numbers starting from B.
For example, given n=3, A=73 and B=272 are Square Friends since 73984=272 2, 74529=2732, and 75076=2742.
Now you are asked to find, for any given n, all the Square Friends within the range where A≤MaxA.
Input Specification:
Each input file contains one test case. Each case gives 2 positive integers: n (≤100) and MaxA (≤106), as specified in the problem description.
Output Specification:
Output all the Square Friends within the range where A≤MaxA. Each pair occupies a line in the format A B. If the solution is not unique, print in the non-decreasing order of A; and if there is still a tie, print in the increasing order of B with the same A. Print No Solution. if there is no solution.
Sample Input 1:
3 85
Sample Output 1:
73 272
78 281
82 288
85 293
Sample Input 2:
4 100
Sample Output 2:
No Solution.
这题题意应该有点难读懂吧,参考以下这位大佬的博客解析吧,这里就不过度赘述了。
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
int n, maxA;
bool check(int A, int B)
{
for (int i = 0; i < n; i++)
{
if (B * B / 1000 != A)
return false;
A++;
B++;
}
return true;
}
int main()
{
bool flag = false;
cin >> n >> maxA;
for (int i = 1; i <= maxA; i++)
{
for (int j = sqrt(i * 1000); j <= sqrt((i + 1) * 1000); j++)
{
if (check(i, j))
{
flag = true;
cout << i << " " << j << endl;
}
}
}
if (flag == false)
cout << "No Solution." << endl;
return 0;
}
7-2 One Way In, Two Ways Out (25 分)
Consider a special queue which is a linear structure that allows insertions at one end, yet deletions at both ends. Your job is to check, for a given insertion sequence, if a deletion sequence is possible. For example, if we insert 1, 2, 3, 4, and 5 in order, then it is possible to obtain 1, 3, 2, 5, and 4 as an output, but impossible to obtain 5, 1, 3, 2, and 4.
Input Specification:
Each input file contains one test case. For each case, the first line gives 2 positive integers N and K (≤10), which are the number of insertions and the number of queries, respectively. Then N distinct numbers are given in the next line, as the insertion sequence. Finally K lines follow, each contains N inserted numbers as the deletion sequence to be checked.
All the numbers in a line are separated by spaces.
Output Specification:
For each deletion sequence, print in a line yes if it is indeed possible to be obtained, or no otherwise.
Sample Input:
5 4
10 2 3 4 5
10 3 2 5 4
5 10 3 2 4
2 3 10 4 5
3 5 10 4 2
Sample Output:
yes
no
yes
yes
自己加了一个测试样例方便你进一步测试:
Sample Input:
6 1
10 2 3 4 5 6
3 2 10 4 6 5
<

最低0.47元/天 解锁文章
1452

被折叠的 条评论
为什么被折叠?



