PAT合集:Colors in Mars ,List Sorting , Median ,Travel Plan

Colors in Mars

简单进制转换,没什么说的。

#include"bits/stdc++.h"
using namespace std;
int main(){
	int val1, val2, val3;
	cin >> val1 >> val2 >> val3;
	string str1 = "  ", str2 = "  ", str3 = "  ";
	char msg[] = "0123456789ABC";
	str1[1] = msg[val1 % 13];
	str1[0] = msg[val1 / 13];
	str2[1] = msg[val2 % 13];
	str2[0] = msg[val2 / 13];
	str3[1] = msg[val3 % 13];
	str3[0] = msg[val3 / 13];
	cout << "#" << str1 << str2 << str3;
 }

Median

利用2个queue来抛掉前(sum - 1) / 2个元素。 放入0x7fffffff防止越界。

#include"bits/stdc++.h"
using namespace std;
int main(){
	queue <int> v1, v2;
	int n1, n2, sum = 0;
	cin >> n1;
	sum += n1;
	for(int i = 0; i < n1; ++i){
		int temp;
		cin >> temp;
		v1.push(temp);
	}
	cin >> n2;
	sum += n2;
	for(int i = 0; i < n2; ++i){
		int temp;
		cin >> temp;
		v2.push(temp);
	}
	int t = (sum - 1) / 2;
	v1.push(0x7fffffff);
	v2.push(0x7fffffff);
	while(t--){
		if(v1.front() < v2.front())
			v1.pop();
		else v2.pop();
	}
	cout << min(v1.front(), v2.front());
}

List Sorting

简单结构排序,没什么说的。

#include"bits/stdc++.h"
using namespace std;
struct stud{
	int id;
	char name[10];
	int score;
};
stud studs[100005];
int n, acc = 0;
bool cmp1(stud a, stud b){
	return a.id < b.id;
}
bool cmp2(stud a, stud b){
	if(strcmp(a.name, b.name) != 0) return strcmp(a.name, b.name) < 0;
	else return a.id < b.id;	
}
bool cmp3(stud a, stud b){
	if(a.score != b.score) return a.score < b.score;
	else return a.id < b.id;	
}
int main(){
	scanf("%d %d", &n, &acc);
	for(int i = 0; i < n; ++i)
		scanf("%d %s %d", &studs[i].id, studs[i].name, &studs[i].score);
	if(acc == 1)
		sort(studs, studs + n, cmp1);
	else if(acc == 2)
		sort(studs, studs + n, cmp2);
	else if(acc == 3)
		sort(studs, studs + n, cmp3);
	for(int i = 0; i < n; ++i)
		printf("%06d %s %d\n", studs[i].id, studs[i].name, studs[i].score);
 }

Travel Plan

dijkstra算法,比较模板。

#include"bits/stdc++.h"
using namespace std;
int costs[600][600];
int diss[600][600];

int oprev[600];
int cost[600];
int dis[600];
int vis[600];
void getans(int p){
	if(p == -1) return;
	else{
		getans(oprev[p]);
		if(oprev[p] != -1) cout << " ";
		cout << p;
	}
}
int main(){
	int n, roadn, from, dist;
	scanf("%d %d %d %d", &n, &roadn, &from, &dist);
	memset(costs, 0x3f, 600 * 2400);
	memset(diss, 0x3f, 600 * 2400);
	memset(vis, 0, 2400);
	memset(dis, 0x3f, 2400);
	memset(oprev, -1, 2400);
	memset(cost, 0x3f, 2400);
	for(int i = 0; i < roadn; ++i){
		int a, b, c, d;
		cin >> a >> b >> d >> c;
		diss[a][b] = diss[b][a] = d;
		costs[a][b] = costs[b][a] = c;
	}
	
	cost[from] = 0;
	dis[from] = 0;
	oprev[from] = -1;
	
	while(true){
		int p = -1;
		int mindis = 0x3f3f3f3f;
		for(int i = 0; i < n; ++i){
			if(mindis > dis[i] && !vis[i]){
				p = i;
				mindis = dis[i];
			}
		}
		if(p == -1) break;
		vis[p] = -1;
		for(int i = 0; i < n; ++i){
			if(diss[i][p] != 0x3f3f3f3f && (diss[i][p] + dis[p] < dis[i] || (diss[i][p] + dis[p] == dis[i] && costs[i][p] + cost[p] < cost[i]))){
				dis[i] = diss[i][p] + dis[p];
				cost[i] = costs[i][p] + cost[p];
				oprev[i] = p;
			}
		}
	}
	getans(dist);
	cout << " " << dis[dist] << " " << cost[dist];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值