7-1 Forever

题目:
“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

AC代码:

#include <bits/stdc++.h>
using namespace std;
struct node{int data,digit;
};
vector<node> res;
bool isprime(int a)
{if(a<=2) return false;int sqr=(int)sqrt(1.0*a);for(int i=2;i<=sqr;i++) if(a%i==0) return false;return true;
}
int gcb(int a,int b)
{if(b==0) return a;return gcb(b,a%b);
}
bool cmp(node a,node b)
{if(a.data!=b.data) return a.data<b.data;else return a.digit<b.digit;
}
int main()
{int k,n,m;cin>>n;for(int i=1;i<=n;i++){int minn=1,maxn=1;scanf("%d%d",&k,&m);res.clear();printf("Case %d\n",i);for(int j=1;j<k;j++) minn*=10;maxn=minn*10;int flag=0;for(int j=minn+9;j<maxn-1;j+=10){int temp=j,temp2=j+1,ans=0,ans1=0;while(temp!=0){ans+=temp%10;temp/=10;}if(ans==m){while(temp2!=0){ans1=ans1+(temp2%10);temp2=temp2/10;}}int maxans=-1;if(ans1!=0) maxans=gcb(m,ans1);if(isprime(maxans)){res.push_back(node{ans1,j});} }sort(res.begin(),res.end(),cmp);if(res.size()==0) printf("No Solution\n");for(int j=0;j<res.size();j++){printf("%d %d\n",res[j].data,res[j].digit);}}return 0;
}

7-2 Merging Linked Lists

题目:
Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→b​m.If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→b​m→a3→a4→bm−1⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L1and L2, plus a positive N (≤10^​5) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next
where Address is the position of the node, Data is a positive integer no more than 10^​5
​​ , and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1

Sample Output:

01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1

AC代码:

#include <bits/stdc++.h>
using namespace std;
struct Node{int data;int add,next,flag;
}node[100001],a[100001],b[100001];
int st1,st2,n;
int main()
{int add,cnt1=0,cnt2=0;cin>>st1>>st2>>n;for(int i=0;i<n;i++){cin>>add;node[add].add=add;cin>>node[add].data>>node[add].next;}for(int p=st1;p!=-1;p=node[p].next) a[cnt1++]=node[p];for(int p=st2;p!=-1;p=node[p].next) b[cnt2++]=node[p];if(cnt1>=2*cnt2){int st=cnt2-1;for(int i=0;i<cnt1;i+=2){if(i!=cnt1-1) printf("%05d %d %05d\n",a[i].add,a[i].data,a[i+1].add);else { printf("%05d %d -1",a[i].add,a[i].data); break;}if(st>=0){printf("%05d %d %05d\n",a[i+1].add,a[i+1].data,b[st].add);if(st==0&&cnt1==2*cnt2) printf("%05d %d -1\n",b[st].add,b[st].data); else printf("%05d %d %05d\n",b[st].add,b[st].data,a[i+2].add);st--;}else if(i+1!=cnt1-1){printf("%05d %d %05d\n",a[i+1].add,a[i+1].data,a[i+2].add);}else printf("%05d %d -1\n",a[i+1].add,a[i+1].data);}}else if(cnt2>=2*cnt1){int st=cnt1-1;for(int i=0;i<cnt2;i+=2){if(i!=cnt2-1)printf("%05d %d %05d\n",b[i].add,b[i].data,b[i+1].add);else { printf("%05d %d -1",b[i].add,b[i].data); break;}if(st>=0){printf("%05d %d %05d\n",b[i+1].add,b[i+1].data,a[st].add);if(st==0&&cnt2==2*cnt1) printf("%05d %d -1\n",a[st].add,a[st].data); else printf("%05d %d %05d\n",a[st].add,a[st].data,b[i+2].add);st--;}else if(i+1!=cnt2-1){printf("%05d %d %05d\n",b[i+1].add,b[i+1].data,b[i+2].add);}else printf("%05d %d -1\n",b[i+1].add,b[i+1].data);}}return 0;
}

7-3 Postfix Expression

题目:
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

data left_child right_child
where data is a string of no more than 10 characters, left_child and right_child are the indices of this node’s left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.



Output Specification:
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.

Sample Input 1:

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output 1:

(((a)(b)+)((c)(-(d))*)*)

Sample Input 2:

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output 2:

(((a)(2.35)*)(-((str)(871)%))+)

AC代码:

#include <bits/stdc++.h>
using namespace std;
struct Node{string data;int lc,rc;
}node[22];
int n,hasht[22];
void postorder(int root)
{if(root==-1) return;printf("(");if(node[root].lc!=-1){postorder(node[root].lc);postorder(node[root].rc);cout<<node[root].data;}else{cout<<node[root].data;postorder(node[root].lc);postorder(node[root].rc);}printf(")");
}
int main()
{cin>>n;for(int i=1;i<=n;i++){cin>>node[i].data>>node[i].lc>>node[i].rc;if(node[i].lc!=-1) hasht[node[i].lc]=1;if(node[i].rc!=-1) hasht[node[i].rc]=1;}int root;for(int i=1;i<=n;i++) if(hasht[i]==0) { root=i;break; }postorder(root);return 0;
}

7-4 Dijkstra Sequence

题目:

Dijkstra’s algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let’s call it Dijkstra sequence, is generated by Dijkstra’s algorithm.

On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers Nv(≤10^3) and Ne(≤10 ^5), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv. Then Ne lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the Nv vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.

Output Specification:
For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.

Sample Input:

5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4

Sample Output:

Yes
Yes
Yes
No

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1001;
const int inf=0x3fffffff;
int G[maxn][maxn];
int d[maxn][maxn],vis[maxn];
int n,m,k,query[maxn],hasht[maxn];
void Dijkstra(int s)
{fill(d[s],d[s]+maxn,inf);fill(vis,vis+maxn,0);d[s][s]=0;for(int i=0;i<n;i++){int u=-1,MIN=inf;for(int j=1;j<=n;j++){if(vis[j]==0&&d[s][j]<MIN){u=j;MIN=d[s][j];}}if(u==-1) return;vis[u]=1;for(int v=1;v<=n;v++){if(vis[v]==0&&G[u][v]!=0){if(d[s][v]>d[s][u]+G[u][v]){d[s][v]=d[s][u]+G[u][v];}}}}
}
int main()
{int u,v,w;cin>>n>>m;for(int i=0;i<m;i++){cin>>u>>v>>w;G[u][v]=G[v][u]=w;}cin>>k;while(k--){cin>>query[0];int ans=0,flag=1;if(hasht[query[0]]==0) Dijkstra(query[0]);hasht[query[0]]=1;for(int i=1;i<n;i++){cin>>query[i];for(int j=i-1;j>=0;j--){if(d[query[0]][query[j]]>d[query[0]][query[i]]){flag=0;break;}}}if(flag) printf("Yes\n");else printf("No\n");}
}

2019秋季PAT甲级题解相关推荐

  1. 2019年9月8日秋季PAT甲级题解A1163(7-4)Dijkstra Sequence

    2019年9月8日秋季PAT甲级题解 A1163(7-4)Dijkstra Sequence (30 分) #include<iostream> #include<vector> ...

  2. 2019秋季PAT甲级考试总结:努力+策略+运气

    鉴于这两天有很多网友联系我问这次考试的题解,所以我干脆就花点时间把C++题解整理出来了,见文末 经过一两个月的备战PAT,在今天终于画上了一个圆满的句号,取得了满分的成绩. 我是在南京的金陵科技学院考 ...

  3. 2019秋季PAT甲级_C++题解

    2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...

  4. 2019秋季PAT甲级考试心得

    第一道题一共四个测试点,做了一个小时,有两个测试点没过,得了12/20分 做题的时候遇到一个问题,就是include<math.h>之后,使用pow(10,n)计算报错具有多个定义之类的( ...

  5. 【PAT甲级题解记录】1148 Werewolf - Simple Version (20 分)

    [PAT甲级题解记录]1148 Werewolf - Simple Version (20 分) 前言 Problem:1148 Werewolf - Simple Version (20 分) Ta ...

  6. 2019年秋季PAT甲级考试记录、经验总结

    在武汉科技大学黄家湖校区考的,非常偏远,在很荒僻的地方,荒僻到公交都要3元的地方(开玩笑~).我去的路上大概花了两个小时,12点半到的,武科机房软.硬件条件很好,跟官网的要求一样,绝对能够满足各种常用 ...

  7. 2022夏PAT甲级题解 by小柳2022.6.7

    公众号题解在此 题目在此 因为某些原因没有报名这次的PAT甲级考试, 所以在上班时间摸鱼写了一下

  8. 2019年秋季PAT甲级考试真题及参考答案

    1160 (20 Forever dfs+回溯剪枝) 题目描述: "Forever number" is a positive integer A with K digits, s ...

  9. 2021年秋季PAT乙级题解(C语言)

    7-1 好数 (15 分) 好数是指由一对正整数 a<b 按照 a^2+ab+b ^2这个规则生成的数,a 和 b 就称为这个好数的源头.例如 91 就是一个好数,因为 5 ^2+5×6+6 ^ ...

  10. 【PAT甲级题解】1097 Deduplication on a Linked List (25分)

    Deduplication意为重复数据消除,Deplicated意为复制,是deduplication的过去式,题干大意为给定一个单链表L要求你对每一个结点的值判断,如果这个值的绝对值是第一次出现,那 ...

最新文章

  1. 通过命令行启动 Microsoft File Transfer Manager
  2. Spring5参考指南:SpringAOP简介
  3. 区块链ppt_区块链研究(附华为百页PPT)
  4. python语言pos_Python自然语言处理(二)--NLTK调用Stanford_NLP_Tools完成NLP任务
  5. python插值算法实现_图像插值算法Opencv+python实现
  6. 传统语音识别(GMM+HMM)
  7. 【C++】C++为什么要引入引用这个复合类型?
  8. win系统CH341驱动安装方法
  9. 【手把手教程】uniapp + vue 从0搭建仿斗鱼虎牙直播App:腾讯云MLVB移动直播实践连麦PK+带货
  10. 手机不能访问html文件,手机如何解决禁止访问网页
  11. 苹果真伪查询_二手MacBook Pro Air等苹果笔记本验货 鉴定 基本方法 流程
  12. 线性代数常用名词详解1
  13. 电影票业务-字节青训营
  14. 数据结构与算法-初识树和图
  15. GPS坐标偏移与转换
  16. 什么是域名服务器?域名服务器的作用是什么?
  17. mysql timestamp类型格式转换
  18. 项目上线前出Bug,测试人该如何调整心态
  19. 了解sprin是什么?有啥用?为什么要用?
  20. vue中的slot(插槽)详解

热门文章

  1. KDD2019 | 开放学术图谱 (OAG): 大规模知识图谱异构实体对齐
  2. 千锋php 靠谱吗,千锋PHP学员心得 长久的坚持不懈才能接近成功
  3. iOS控件使用和多样布局
  4. MYSQL之STRAIGHT_JOIN
  5. Mockito使用简介
  6. 软件测试这个工作入门难不难?
  7. 8421码的加减矫正
  8. 川农《劳动与社会保障法(本科)》21年12月作业考核
  9. 福师计算机应用基础在线作业,福师《计算机应用基础》在线作业二 下列软件中属于应用软件的是...
  10. 华为实验21-OSPF的DR与BDR