PAT甲级1062,1063解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1062 Talent and Virtue (25 point(s))

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.

Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.

Input Specification:

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤10​5​​), the total number of people to be ranked; L (≥60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade

where ID_Number is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.

Output Specification:

The first line of output must give M (≤N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.

Sample Input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

Sample Output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

题目大意:人有三六九等,把每个人属性设为id,品质和天赋,有两个分数线,一个最低,一个优秀,分成几类人,第一类人的两类分数全部优秀,根据总分来排名。第二类人的天赋没有优秀,但是品质优秀的,也是根据总分排名,第三类人两个分数全部低于优秀,但是品质不低于天赋,第四类人剩下的除了不及格的都是。不及格的都不算人不参与统计了。

解题思路:水题,主要考英文阅读仔细看就是了,输出在后面有限制,如果总分相同根据品质,如果品质也相同根据id递增。

基本上就是分成四个然后排个序就好了。

代码如下:

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
struct per{
	string id;
	int virtue;
	int talent;
	int total;
};
per cur[100005];
vector<per> sage;
vector<per> noblemen;
vector<per> foolmen;
vector<per> rest;
bool cmp(per a, per b) {
	if (a.total > b.total)return true;
	else if (a.total == b.total) {
		if (a.virtue > b.virtue) {
			return true;
		}
		else if (a.virtue == b.virtue) {
			return a.id < b.id;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}
void Print(vector<per> p) {
	for (int i = 0; i < p.size(); i++) {
		cout << p[i].id << " " << p[i].virtue << " " << p[i].talent << endl;
	}
}
int main() {
	int N, L, H;
	scanf("%d %d %d", &N, &L, &H);
	for (int i = 0; i < N; i++) {
		cin >> cur[i].id >> cur[i].virtue >> cur[i].talent;
		cur[i].total = cur[i].virtue + cur[i].talent;
		if (cur[i].virtue >= H&&cur[i].talent >= H) {
			sage.push_back(cur[i]);
		}
		else if (cur[i].virtue >= H&&cur[i].talent < H&&cur[i].talent >= L) {
			noblemen.push_back(cur[i]);
		}
		else if (cur[i].virtue < H&&cur[i].talent < H&&cur[i].talent >= L&&cur[i].virtue >= L&&cur[i].virtue >= cur[i].talent) {
			foolmen.push_back(cur[i]);
		}
		else if (cur[i].virtue<L||cur[i].talent<L) {
			continue;
		}
		else
			rest.push_back(cur[i]);
	}   
	sort(sage.begin(), sage.end(), cmp);
	sort(noblemen.begin(), noblemen.end(), cmp);
	sort(foolmen.begin(), foolmen.end(), cmp);
	sort(rest.begin(), rest.end(), cmp);
	cout << sage.size() + noblemen.size() + foolmen.size() + rest.size() << endl;
	Print(sage);
	Print(noblemen);
	Print(foolmen);
	Print(rest);
	return 0;
}

1063 Set Similarity (25 point(s))

Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (≤50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤10​4​​) and followed by M integers in the range [0,10​9​​]. After the input of sets, a positive integer K (≤2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

Sample Output:

50.0%
33.3%

题目大意:给一堆集合(有些并不符合相异性),给出任意两个集合的查询,算相似度,相似度计算是交集总数/并集总数的百分比一位小数表示。

解题思路:用stl的set_intersection和set_union即可求出交集并集。但注意这两个函数的参数必须是两个有序容器和支持pushback的容器名。用一下后面的操作都是无脑了,虽然我试了一下手摸,也是可以过的拉。那肯定是模板效率更高。

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
vector<set<int>> a(55);
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int m;
		scanf("%d", &m);
		for (int j = 0; j < m; j++) {
			int cur;
			scanf("%d", &cur);
			a[i].insert(cur);
		}
	}
	int q;
	scanf("%d", &q);
	while (q--) {
		int t, p;
		scanf("%d %d", &t, &p);
		vector<int> cc; 
		set_intersection(a[t - 1].begin(), a[t - 1].end(), a[p - 1].begin(), a[p - 1].end(), back_inserter(cc));
		vector<int> res;
		set_union(a[t - 1].begin(), a[t - 1].end(), a[p - 1].begin(), a[p - 1].end(), back_inserter(res));
		printf("%.1lf", cc.size() * 100.0 / res.size());
		cout << "%" << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值