本文主要是介绍PAT甲级1113,1116解题报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 A1 and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that ∣n1−n2∣ is minimized first, and then ∣S1−S2∣ is maximized.
Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤105), 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 231.
Output Specification:
For each case, print in a line two numbers: ∣n1−n2∣ and ∣S1−S2∣, 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 (≤104), 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;
}
这篇关于PAT甲级1113,1116解题报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!