PAT-2019年秋季考试-甲级

总分: 80 / 100
编程题总分: 80 / 100

7-1 Forever未作答 得分: 0 / 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

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

惨痛的教训~
第一题不能死磕简单办法,打表暴力才是应付低分值数学题的王道!

/*
* @Author: Achan
* @Date:   2019-09-10 16:03:39
* @Last Modified by:   Achan
* @Last Modified time: 2019-09-10 22:39:38
*/
#include
using namespace std;
typedef long long ll;
typedef struct node
{
	int n,A;
}node;

inline int sum_dig(ll n)
{
	int ret = 0;
	while(n)
	{
		ret += (n%10);
		n/=10;
	}
	return ret;
}

bool cmp(node a, node b)
{
	if(a.n == b.n)
		return  a.A < b.A;
	return a.n < b.n;
}
inline bool check(int n,int m)
{
	int g = __gcd(n,m);
	if(g <= 2) return false;
	for (int i = 2; i*i < g; ++i)
	{
		/* code */
		if(g%i == 0) return false;
	}
	return true;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int k,m;
		cin >> k >> m;
		cout << "Case "<<i+1 << endl;

		k-=2;
		std::vector<node> ans;
		for(int i = (int) pow(10,k-1); i < (int )pow(10,k); i++)
		{
			int tn =  sum_dig(i+1);
			if(sum_dig(i) + 18 == m && check(tn, m)) {
				node t;
				t.n = tn;
				t.A = i;
				ans.push_back(t);
			}
		}
		if(!ans.size()) {
			cout <<"No Solution" << endl;
			return 0;
		} 
		sort(ans.begin(),ans.end(),cmp);
		for (int i = 0; i < (int)ans.size(); ++i)
		{
			cout << ans[i].n << " " << ans[i].A << "99" << endl;
		}
	}  
    return 0;
}

7-2 Merging Linked Lists答案正确 得分: 25 / 25
Given two singly linked lists L
​1
​​ =a
​1
​​ →a
​2
​​ →⋯→a
​n−1
​​ →a
​n
​​ and L
​2
​​ =b
​1
​​ →b
​2
​​ →⋯→b
​m−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 a
​1
​​ →a
​2
​​ →b
​m
​​ →a
​3
​​ →a
​4
​​ →b
​m−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 L
​1
​​ and L
​2
​​ , 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

·····················································································································

emmm。。没啥说的,按照题目要求,水一水。用双链表控制输出 ??

#include
using namespace std;
const int maxn = 1e6 + 5;

int main(void)
{
   // freopen("in.txt","r",stdin);

    string f1, f2;
    cin >> f1 >> f2;
    int T; cin >> T;
    map <string, pair<int, string> > mp;
    map <string, string> link;
    while(T--)
    {
        string n,m;
        int k;
        cin >> n >> k >> m;
        mp[n] = make_pair(k,m);
    }
    int len1 = 0, len2 =0;
    string cu = f1;
    while(cu!="-1")
    {
        len1++;
        cu = mp[cu].second;
    }
    cu = f2;
    while(cu!="-1")
    {
        len2++;
        cu = mp[cu].second;
    }
    if(len1  < 2*len2)
    {
        string temp = f1;
        f1 = f2;
        f2 = temp;
    }
    link[f2] = "-1";
    for(string cur = f2; cur != "-1"; cur = mp[cur].second)
    {
        link[mp[cur].second] = cur; //前缀链
    }
    //vector < string, pair > ans;
    string cur = f1, bc = "-1";
    bool now  = true;
    while(cur!="-1" || link[bc]!="-1")
    {
        if(now){
                cout << cur << " " << mp[cur].first << " " << mp[cur].second << endl;
                cur = mp[cur].second;
                if(cur == "-1") break;
                cout << cur << " " << mp[cur].first << " ";
                if(link[bc] != "-1") cout <<  link[bc];
                else cout <<  mp[cur].second;
                cout << endl;

                cur = mp[cur].second;
                if(cur == "-1" && link[bc] == "-1") break; 
            }
        else if(link[bc]!= "-1" ){
                cout << link[bc] <<" "<< mp[link[bc]].first<< " ";
                if(cur!="-1") cout<< cur << endl;
                else {
                	cout<< "-1" << endl;
                	break;
                }
                bc = link[bc]; //链式向前
        	}

        now = (!now);
    }
    return 0;
}

测试点 结果 耗时 内存
0 答案正确 3 ms 464KB
1 答案正确 3 ms 424KB
2 答案正确 2 ms 352KB
3 答案正确 3 ms 416KB
4 答案正确 375 ms 16296KB

·····················································································································

7-3 Postfix Expression答案正确 得分: 25 / 25
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.

infix1.JPG infix2.JPG
Figure 1 Figure 2
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)+)(©(-(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)%))+)

第一个样例 -(d) 老是输出(d)- ,改了改,好了。。。

/*
* @Author: Achan
* @Date:   2019-09-10 15:32:13
* @Last Modified by:   Achan
* @Last Modified time: 2019-09-10 23:04:54
*/
#include
using namespace std;


std::vector<pair<string , pair<int, int > > > v(22);

void postFix(int root)
{
	cout << "(";
	if(v[root].second.first != -1) postFix(v[root].second.first);
	if(v[root].second.first != -1 && v[root].second.second != -1) postFix(v[root].second.second);
	if(root != -1) cout << v[root].first;
	if(v[root].second.first == -1) {
		if(v[root].second.second != -1) postFix(v[root].second.second);
	}

	cout << ")";
}
int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	cin >> n;
	int sumi = 0, sumn = 0;
	for(int i = 1; i <= n; i++)
	{
		string ch;
		int l, r;
		cin >> ch >> l >> r;
		v[i].first = ch;
		v[i].second.second = r;
		v[i].second.first =  l; 
		sumi += i;
		if(l>=0) sumn += l;
		if(r>=0) sumn += r;
	}
	postFix(sumi - sumn);

    
#ifdef LOCAL
    Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif   
    return 0;
}

测试点 结果 耗时 内存
0 答案正确 31 ms 39396KB
1 答案正确 31 ms 39396KB
2 答案正确 30 ms 39424KB
3 答案正确 36 ms 39396KB
4 答案正确 31 ms 39424KB

·····················································································································

7-4 Dijkstra Sequence答案正确 得分: 30 / 30
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 N
​v
​​ (≤10
​3
​​ ) and N
​e
​​ (≤10
​5
​​ ), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N
​v
​​ .

Then N
​e
​​ 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 N
​v
​​ 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

额,基本上就是模板了。维护最短集的一般模板忘了咋写,就按熟悉的堆优化过了。

#include
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 5;
const int INF = 0x3f3f3f3f;


typedef struct 
{
	int to;
	int w;
	int next;
}node;
node edge[(int)(1e5 + 1) << 1];
int top; 
int head[maxn];
inline void add_edge(int u, int v, int w)
{
	edge[top].to = v;
	edge[top].w = w;
	edge[top].next = head[u];
	head[u] = top;
	top++;
}
inline void init()
{
   // memset(vis,0,sizeof(vis));
	top = 0;
	memset(head,-1,sizeof(head));

}

vector <int> seq(maxn);
int vis[maxn];
int n,e;
ll dis[maxn];

bool Dijkstra(int s, int idx)
{
   	memset(vis,0,sizeof(vis));
    fill(dis,dis+n+1,INF);  
    dis[s] = 0;
    priority_queue<pair<ll,int> > q;
    q.push(make_pair(0,s));
    while(!q.empty())
    {
        int t = q.top().second;
        q.pop();
        if(vis[t]) continue;
        vis[t] = 1; 
        //增加如下判断
       	if(dis[t] == dis[seq[idx]]){  //从第一第二组样例可以看出: 满足最短边的所有点都可以加入集合。
//	        t = seq[idx]; //严格按照给的序列更新 
       		idx++;
       		if(idx == n) break;
       	}
       	else{
       		return false;
       	}
       	//剩下的都是模板啦~
     		
        for(int i = head[t]; ~i; i = edge[i].next)
        {
        	int to = edge[i].to;
        	int w = edge[i].w;
            if(!vis[to] && dis[to] > dis[t] + w)
            {
                dis[to] = dis[t] + w;
                q.push(make_pair(-dis[to],to));
            }
        }

    }
    return true; 
}
int main(void)
{
    //freopen("in.txt","r",stdin);
    cin.tie(0), cout.tie(0);
    cin >> n >> e;
    init();
    for(int i = 0; i < e; i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        add_edge(u,v,w), add_edge(v,u,w);
    }
    int k;
    cin >> k;
    while(k--){
        for (int i = 0; i < n; ++i)
        {
	        cin >> seq[i];
		}        
        puts( Dijkstra(seq[0],0)? "Yes": "No");
    }
    return 0;
}


测试点 结果 耗时 内存
0 答案正确 2 ms 424KB
1 答案正确 3 ms 424KB
2 答案正确 3 ms 388KB
3 答案正确 292 ms 2472KB

你可能感兴趣的:(PAT(甲级))