PAT甲级1113,1116解题报告

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

1113 Integer Set Partition (25 point(s))

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A​1​​ and A​2​​ of n​1​​ and n​2​​ numbers, respectively. Let S​1​​ and S​2​​ denote the sums of all the numbers in A​1​​ and A​2​​, respectively. You are supposed to make the partition so that ∣n​1​​−n​2​​∣ is minimized first, and then ∣S​1​​−S​2​​∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤10​5​​), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2​31​​.

Output Specification:

For each case, print in a line two numbers: ∣n​1​​−n​2​​∣ and ∣S​1​​−S​2​​∣, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output 2:

1 9359

题目大意:给一个数组,分开两份,要求元素之差最小的前提下,使两者各自求和的差距最大。

解题思路:没什么讲究水题,无非就是奇数个偶数个,分开搞一搞就好了。

#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;
int a[100005];
int main() {
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%d", &a[i]);
	}
	sort(a, a + N);
	if (N % 2 == 0) {
		int sum1 = 0;
		int sum2 = 0;
		for (int i = 0; i < N / 2; i++) {
			sum1 += a[i];
		}
		for (int i = N / 2; i < N; i++) {
			sum2 += a[i];
		}
		printf("0 %d\n", abs(sum2 - sum1));
	}
	else {
		int sum1 = 0;
		int sum2 = 0;
		if (a[N / 2] <0) {
			for (int i = 0; i <= N / 2; i++) {
				sum1 += a[i];
			}
			for (int i = N / 2 + 1; i < N; i++) {
				sum2 += a[i];
			}
		}
		else {
			for (int i = 0; i < N / 2; i++) {
				sum1 += a[i];
			}
			for (int i = N / 2; i < N; i++) {
				sum2 += a[i];
			}
		}
		printf("1 %d\n", abs(sum2 - sum1));
	}

	return 0;
}

1116 Come on! Let's C (20 point(s))

"Let's C" is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

  • 0、 The Champion will receive a "Mystery Award" (such as a BIG collection of students' research papers...).
  • 1、 Those who ranked as a prime number will receive the best award -- the Minions (小黄人)!
  • 2、 Everyone else will receive chocolates.

Given the final ranklist and a sequence of contestant ID's, you are supposed to tell the corresponding awards.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​4​​), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant's ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID's.

Output Specification:

For each query, print in a line ID: award where the award is Mystery Award, or Minion, or Chocolate. If the ID is not in the ranklist, print Are you kidding? instead. If the ID has been checked before, print ID: Checked.

Sample Input:

6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample Output:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

题目大意:给一个排名,给一个查询列表,对于每一个查询,如果排名是1则Myster。。

如果排名是素数,那么输出Minion,如果排名是别的,那么输出Chocolate,如果这个人不存在,那么就输入Are you kidding?另外对于已经查过的且是存在的,那么输出Checked。

解题思路:用两个map查询就好了

#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;
bool judge(int x) {
	if (x == 1)return false;
	else if (x == 2)return true;
	else {
		bool flag=true;
		for (int i = 2; i <= sqrt(x) + 1; i++) {
			if (x%i == 0) {
				flag = false;
				break;
			}
		}
		return flag;
	}
	
}
map<int, int> a;
map<int, bool> b;
int main() {
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int tmp;
		scanf("%d", &tmp);
		a[tmp] = i + 1;
		b[tmp] = false;
	}
	int K;
	scanf("%d", &K);
	for (int i = 0; i < K; i++) {
		int tmp;
		scanf("%d", &tmp);
		if (b[tmp]) {
			printf("%04d: Checked\n", tmp);
		}
		else {
			
			printf("%04d: ", tmp);
			if (a[tmp] > 0) {
				b[tmp] = true;
				if (a[tmp] == 1) {
					printf("Mystery Award\n");
				}
				else if (judge(a[tmp])) {
					printf("Minion\n");
				}
				else {
					printf("Chocolate\n");
				}
			}
			else {
				printf("Are you kidding?\n");
			}
		}
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值