浙江大学计算机与软件学院2021年考研复试上机模拟练习

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
<
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值