0
点赞
收藏
分享

微信扫一扫

PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)

洛茄 2022-04-13 阅读 68

1001 A+B Format (20 分)

题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种。

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int a, b, c;
	string s, ans, flag;
	cin >> a >> b;
	c = a + b;
	if (c < 0) flag = "-";
	s = to_string (c);
	reverse (s.begin(), s.end ());
	
	for (int i = 0;i < s.size() && s[i] != '-';i ++) {
		if (i % 3 == 0 && i) ans += ",";
		ans += s[i];		
	}
	
	reverse (ans.begin (), ans.end ());
	
	cout << flag;
	cout << ans << endl;
	return 0;
}

1002 A+B for Polynomials (25 分)

题目大意:两个多项式相加,每一行第一个数表示多项式的非零项个数,后面跟着非零项,第一个是阶数,第二个是系数。最后以相同的格式输出相加后的多项式,保留小数点后1位。

我是蠢蛋,杀鸡用了宰牛刀……

#include<bits/stdc++.h>
#define PID pair <int, double>
using namespace std;
const int N = 1100;
vector <PID> ans;
int ka, kb, a, mx = 0, mn = N;
double c1[N], c2[N], b;

bool cmp (PID x, PID y) {
	return x.first > y.first;
}

int main()
{
	cin >> ka;
	for (int i = 0;i < ka;i ++) {
		cin >> a >> b;
		c1[a] = b;
		mn = min (a, mn), mx = max (a, mx);
	}
	cin >> kb;
	for (int i = 0;i < kb;i ++) {
		cin >> a >> b;
		c2[a] = b;
		mn = min (a, mn), mx = max (a, mx);
	}
	
	for (int i = mn;i <= mx;i ++) 
		if (c1[i] + c2[i] != 0) ans.push_back ({i, c1[i] + c2[i]});

	sort (ans.begin (), ans.end (), cmp);
	
	cout << ans.size();
	for (int i = 0;i < ans.size();i ++) 
		printf (" %d %.1lf", ans[i].first, ans[i].second);
	return 0;
}

我的代码:

#include<bits/stdc++.h>
#define PID pair <int, double>
using namespace std;

int ka, kb, e;
double c;
vector <PID> ita, itb, ans;

bool cmp (PID a, PID b) {
	return a.first > b.first;
}

int main()
{
	cin >> ka;
	for (int i = 0;i < ka;i ++) cin >> e >> c, ita.push_back ({e, c});
	cin >> kb;
	for (int i = 0;i < kb;i ++) cin >> e >> c, itb.push_back ({e, c});
	
	sort (ita.begin (), ita.end (), cmp);
	sort (itb.begin (), itb.end (), cmp);
	
	for (int i = 0, j = 0;i < ka;i ++) {
		while (j < kb && itb[j].first > ita[i].first) ans.push_back (itb[j]), j ++;
		if (j < kb && itb[j].first == ita[i].first) {
			if (itb[j].second != -ita[i].second) 
				ans.push_back ({ita[i].first, ita[i].second + itb[j].second});
			j ++;
		} else {
			ans.push_back (ita[i]);
		}
		
		while (i == ka-1 && j < kb) {
			ans.push_back (itb[j]);
			j ++;
		}
	}
	
	cout << ans.size();
	for (int i = 0;i < ans.size(); i++) 
		printf (" %d %.1lf", ans[i].first, ans[i].second);
		

	return 0;
}

1003 Emergency (25 分)

题目大意:每个点存在一定的人数,每条边存在长度,问从s到t最短路个数,在保证路径最短的基础上能遇到最多多少个人。

写了三遍都没AC,看了柳神的发现小错误(注释部分),这里错过第二回了这是。

#include<bits/stdc++.h>
using namespace std;
const int N = 510;

int n, m, s, t;
int a, b, c, ans;
int e[N][N];
int g[N]; // i点处的人数 
int h[N]; // s到i最多聚集的人数 
int st[N]; // 是否以i节点松弛过 
int d[N]; // s到i的最短距离 
int p[N]; // s到i最短路径的个数 

void Dijkstra () {
	
	memset (d, 0x3f, sizeof d);
	d[s] = 0;
	p[s] = 1;
	h[s] = g[s];
	
	int T = n;
	while (-- T) {
		int k = -1;
		for (int i = 0;i < n;i ++) 
			if (!st[i] && (k == -1 || d[i] < d[k])) 
				k = i;
		
		st[k] = 1;
		
		for (int i = 0;i < n;i ++) {
			if (!st[i] && e[k][i]) {
				if (d[i] > d[k] + e[k][i]) {
					d[i] = d[k] + e[k][i];
					h[i] = h[k] + g[i];
					p[i] = p[k]; // ! 不是设置为1 
				} else if (d[i] == d[k] + e[k][i]) {
					p[i] += p[k]; // ! 不是等于 p[k] + 1 
					h[i] = max (h[i], h[k] + g[i]);
				}
			}
		}
	}
}

int main()
{
	cin >> n >> m >> s >> t;
	
	for (int i = 0;i < n;i ++) cin >> g[i];
	
	while (m --) {
		cin >> a >> b >> c;
		e[a][b] = c;
		e[b][a] = c;
	}

	Dijkstra ();
	
	cout << p[t] << ' ' << h[t];

	return 0;
}

1004 Counting Leaves (30 分)

题目大意:先输入节点个数n和下面要输入的行数m;每行先输入一个节点编号,再输入该节点的直接子节点数,再输入子节点编号。最后输出树的每一层的叶子节点数量。

#include<bits/stdc++.h>
using namespace std;
const int N = 500;

int n, m, root, maxdp;
int q[N], dp[N], ans[N], st[N];
vector <int> a[N];


void bfs () {
	int tt = -1, hh = 0;
	q[++ tt] = root;
	dp[root] = 1;
	
	while (tt >= hh) {
		int t = q[hh ++];
		maxdp = max (dp[t], maxdp);
		
		for (int i = 0;i < a[t].size();i ++) {
			q[++ tt] = a[t][i];
			dp[a[t][i]] = dp[t] + 1;
		}
	}
}

int main()
{
	cin >> n >> m;
	while (m --) {
		int id, k, child;
		cin >> id >> k;
		while (k --) cin >> child, a[id].push_back (child), st[child] = 1;
	}	
	
	for (int i = 1;i <= n;i ++) if (!st[i]) root = i;
	
	bfs ();
	
	for (int i = 1;i <= n;i ++) 
		if (!a[i].size()) 
			ans[dp[i]] ++;
		
	int flag = 0;	
	for (int i = 1;i <= maxdp;i ++) {
		if (flag) cout << ' ';
		flag = 1;
		cout << ans[i];
	}
	
	return 0;
}

1005 Spell It Right (20 分)

题目大意:将输入的数的每一位加起来,得到和后从高位到低位输出和的每一位对应的英语单词。

坑点:和为0,要输出zero。

#include<bits/stdc++.h>
using namespace std;

string mp[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int sum;
string s;
vector <string> ans;

int main()
{
	cin >> s;
	for (int i = 0;i < s.size();i ++)
	sum += s[i]	- '0';
	
	while (sum) {
		ans.push_back (mp[sum % 10]);
		sum /= 10;
	}
	
	reverse (ans.begin (), ans.end ());
	if (ans.size()) {
		cout << ans[0];
		for (int i = 1;i < ans.size();i ++)
		cout << ' ' << ans[i];
	} else {
		cout << "zero"; // 一个测试点 
	}
	

	return 0;
}
举报

相关推荐

0 条评论