【PAT甲级 2019 9月.8号】 满分题解 -- 很久没写了 考研期间算是对其一个阶段告落的总结

本文提供了PAT甲级考试2019年9月部分试题的满分解,包括Forever Number问题的DFS解决方法,Merge Linked Lists的链表操作,Postfix Expression的后缀表达式生成,以及Dijkstra Sequence的最短路径判断。详细解析了每道题目的输入输出规格及解题思路。
摘要由CSDN通过智能技术生成

考前两个月疯狂刷题155 * 1.5 + 晴神 20 + 模拟题 * 4 时间投入的可有点大,初试冲啊

7-1 Forever (20 分)

"Forever number" is a positive integer A with K digits, satisfying the following constrains:

  • the sum of all the digits of A is m;
  • the sum of all the digits of A+1 is n; and
  • the greatest common divisor of m and n is a prime number which is greater than 2.

Now you are supposed to find these forever numbers.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3<K<10) and m (1<m<90), of which the meanings are given in the problem description.

Output Specification:

For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution

第一题比较有意思,仔细读读题目很容易想到DFS深搜K位数字A并验证A+1之和与A各位数字的最大公约数是大于2的质数

(要不是考试思路太乱 。。。。一个小时就AK)  实际上菜的一笔 ,写完第四题才回来刚第一题,调了一下就过了样例,95分。时间还剩一个小时我开始慌了 70+人过了100分。。。。在坚持不懈的查找bug之下发现判断质数居然跳过了质因数2。怎么说?还不是我自己思路混乱,看到啥写啥,o(︶︿︶)o 唉

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define sget(s) atoi( s.c_str() )
const int M = 1e3;
const int INF = 0x3ffffff;
typedef long long ll;
struct Node{//可以用pair排序
    ll a, b;
    Node(ll c, ll d){ a =  c; b= d;}
};
int cmp(Node a, Node b){
    if(a.a != b.a) return a.a < b.a;
    else return a.b < b.b;
}
vector< Node > ans;
ll num[20] , k, m, n, a;
ll getnum(){//dfs深搜的K位数字
    ll tmp = 0;
    for(ll i = 0;i < k; i++){
        tmp *= 10;
        tmp += num[i];
    }
    return tmp;
}
ll getsum(){
    ll sum = 0;//k位之和
    for(ll i = 0; i < k; i++)
        sum += num[i];
    return sum;
}
ll judge(ll &add2){//判断是否合法
    ll tmp = getnum() + 1, p0 = 0, sum2 = 0, sum1 = getsum();
    while(tmp){
        sum2 += tmp % 10;
        tmp /= 10;
    }
//    tmp = getnum() + 1;
    add2 = sum2;
//    if(tmp < 199999) cout << tmp << ' ' <<sum1 << ' ' << sum2 << endl;
    ll more = 2;
    for( ll i  = 3; i<= min(sum2, sum1 ); i++ ){
        if( sum2 % i == 0 && sum1 % i == 0)
            more = i;
    }
//    if(tmp < 199999) cout <&l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值