PAT甲级1121,1124解题报告

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

1121 Damn Single (25 point(s))

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤50,000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (≤ 10,000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888

题目大意:把一个数组里的成对的全部去掉把单身狗留下

解题思路:存个双向的哈希表,遍历数组标记每个数组成对次数,如果下标为2,那就不是单身狗。

#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;
map<int, int> a;
vector<int> res;
vector<int> cur;
map<int, int> b;
int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int x,y;
		scanf("%d %d", &x,&y);
		a[x] = y;
		a[y] = x;
	}
	int T;
	scanf("%d", &T);
	for (int i = 0; i < T; i++) {
		int x;
		scanf("%d", &x);
		cur.push_back(x);
		b[x]++;
		b[a[x]]++;
	}
	for (int i = 0; i < T; i++) {
		if (b[cur[i]] == 1)
			res.push_back(cur[i]);
	}
	sort(res.begin(), res.end());

		cout << res.size() << endl;
		for (int i = 0; i < res.size(); i++) {
			if (i != res.size() - 1) {
				printf("%05d ", res[i]);
			}
			else
				printf("%05d\n", res[i]);
		}
	

	return 0;
}

1124 Raffle for Weibo Followers (20 point(s))

John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integers M (≤1000), N and S, being the total number of forwards, the skip number of winners, and the index of the first winner (the indices start from 1). Then M lines follow, each gives the nickname (a nonempty string of no more than 20 characters, with no white space or return) of a follower who has forwarded John's post.

Note: it is possible that someone would forward more than once, but no one can win more than once. Hence if the current candidate of a winner has won before, we must skip him/her and consider the next one.

Output Specification:

For each case, print the list of winners in the same order as in the input, each nickname occupies a line. If there is no winner yet, print Keep going... instead.

Sample Input 1:

9 3 2
Imgonnawin!
PickMe
PickMeMeMeee
LookHere
Imgonnawin!
TryAgainAgain
TryAgainAgain
Imgonnawin!
TryAgainAgain

Sample Output 1:

PickMe
Imgonnawin!
TryAgainAgain

Sample Input 2:

2 3 5
Imgonnawin!
PickMe

Sample Output 2:

Keep going...

题目大意:模拟微博抽奖。

解题思路:其实和队列类似吧,但实际不用队列做,基本上就按顺序模拟一下就好了

#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<string> cur;
vector<string> res;
int main()
{
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	for (int i = 0; i < a; i++) {
		string tmp;
		cin >> tmp;
		cur.push_back(tmp);
	}
	if (a < c) {
		cout << "Keep going..." << endl;
	}
	else {
		for (int i = c-1; i < a;) {
			if (count(res.begin(),res.end(),cur[i])==0) {
				res.push_back(cur[i]);
				//cout << cur[i] << endl;
				i = i + b;
			}
			else {
				i = i + 1;
			}
		}
		for (int i = 0; i < res.size(); i++) {
		cout << res[i] << endl;
	}
	}
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值