2020 PAT 甲级冬季题解

第一题

#include<iostream>
using namespace std;
int n;
int a = 0, b = 1, c;
int main() {
	scanf("%d", &n);
	while(true) {
		if(a <= n && b >= n) {
			printf("%d\n", (n - a) <= (b - n) ? a : b);
			break;
		}
		c = b;
		b += a;
		a = c;
	}
	return 0;
}

第二题

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main() {
	string s, t, res = "", ans;
	int flag = 1;
	cin >> s >> t;
	for(int i = 0; i < s.length(); ++i) {
		if(s[i] == t[0]) {
			res = res + s[i];
			int j, k;
			for(j = i + 1, k = 1; j < s.length() && k < t.length(); ++j) {
				res = res + s[j];
				if(s[j] == t[k]) ++k;
			}
			if(k == t.length() && res.length() < ans.length() || flag) {
				ans = res;
				flag = 0;
			}
			res = "";
		}
	}
	cout << ans;
	return 0;
}

第三题

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e5;
int pre[N], last[N], vis[N];
void find(int x) {
	if(pre[x] != -1) {
		find(pre[x]);
		printf("%04d->", pre[x]);
	}
}
int main() {
	int n;
	scanf("%d", &n);
	getchar();
	fill(pre, pre + N, -1);
	string s, t;
	getline(cin, t);
	vis[0] = 1;
	for(int i = 1; i < n; ++i) {
		getline(cin, s);
		int a = stoi(s), c = 0;
		while(s[c] == ' ') ++c;
		last[c] = a;
		pre[a] = last[c - 1];
		vis[a] = 1;
	}
	cin >> n;
	int x;
	for(int i = 0; i < n; ++i) {
		cin >> x;
		if(vis[x]) {
			find(x);
			printf("%04d\n", x); 
		} else {
			printf("Error: %04d is not found.", x);
		}
	}
	return 0;
}

第四题

#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stdlib.h>
using namespace std;
struct node {
	vector<int> rea;
	int pr;
};
vector<node> t;
int vis[21];
vector<int> mater, product;
bool cmp(node &a, node &b) {
	int len = min(a.rea.size(), b.rea.size());
	for(int i = 0; i < len; ++i) if(a.rea[i] != b.rea[i]) return a.rea[i] < b.rea[i];
	return true;
}
int main() {
	int n, m, k;
	scanf("%d", &n);
	mater.resize(n);
	for(int i = 0; i < n; ++i) {
		scanf("%d", &mater[i]);
		++vis[mater[i]];
	}
	scanf("%d", &m);
	product.resize(n);
	for(int i = 0; i < m; ++i) {
		node tt;
		scanf("%d", &product[i]);
		tt.pr = product[i];
		tt.rea.push_back(product[i]);
		t.push_back(tt);
	} 
	scanf("%d", &k);
	string s, pre;
	for(int i = 0; i < k; ++i) {
		node tt;
		while(true) {
			cin >> s;
			if(s == "+" || s == "->") {
				tt.rea.push_back(stoi(pre));
			}
			char c = getchar();
			if(c == '\n') {
				tt.pr = stoi(s);
				break;
			}
			pre = s;
		}
		t.push_back(tt);
	}
	sort(t.begin(), t.end(), cmp);
	for(int i = 0; i < m; ++i) {
		for(int j = 0; j < t.size(); ++j) {
			if(product[i] == t[j].pr) {
				bool flag = true;
				for(int r = 0; r < t[j].rea.size(); ++r) if(!vis[t[j].rea[r]]) {
					flag = false;
					break;
				}
				if(flag) {
					for(int r = 0; r < t[j].rea.size(); ++r) {
						--vis[t[j].rea[r]];
						if(r) cout << " + ";
						printf("%02d", t[j].rea[r]); 
					}
					printf("-> %02d\n", t[j].pr);
					break;
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值