PAT甲级简单题最后冲刺

40 篇文章 0 订阅
35 篇文章 0 订阅

A1001

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −10​6​​≤a,b≤10​6​​. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

从后往前倒数即可,注意两个边界条件 

#include<cstdio>
#include<string>
using namespace std;
int main() {
	int a, b;
	scanf("%d%d", &a, &b);
	string rst = to_string(a + b);
	int length = rst.length();
	for (int i = 0; i < length; i++) {
		printf("%c", rst[i]);
		if ((length - 1 - i) % 3 == 0 && i != (length - 1) && rst[i] != '-') {
			printf(",");
		}
	}
	return 0;
}

 

A1002

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

c[exp] = coef, 注意maxn是幂指数最大可能取多少,而不是k的值 

#include<cstdio>

const int maxn = 1010;
double c[maxn] = { 0.0 };

int main() {
	int k1, k2;
	int exp;
	double coef; //int and double
	scanf("%d", &k1);
	while (k1--) {
		scanf("%d%lf", &exp, &coef);
		c[exp] += coef;
	}
	scanf("%d", &k2);
	while (k2--) {
		scanf("%d%lf", &exp, &coef);
		c[exp] += coef;
	}
	int cnt = 0;
	for (int i = maxn - 1; i >= 0; i--) {
		if (c[i] != 0) {
			cnt++;
		}
	}
	printf("%d", cnt);
	for (int i = maxn - 1; i >= 0; i--) {
		if (c[i] != 0.0) {
			printf(" %d %.1f", i, c[i]);
		}
	}

	return 0;

}

 

1152 Google Recruitment (20分)

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural constant e. The person who could find this prime number could go to the next step in Google's hiring process by visiting this website.

prime.jpg

The natural constant e is a well known transcendental number(超越数). The first several digits are: e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921... where the 10 digits in bold are the answer to Google's question.

Now you are asked to solve a more general problem: find the first K-digit prime in consecutive digits of any given L-digit number.

Input Specification:

Each input file contains one test case. Each case first gives in a line two positive integers: L (≤ 1,000) and K (< 10), which are the numbers of digits of the given number and the prime to be found, respectively. Then the L-digit number N is given in the next line.

Output Specification:

For each test case, print in a line the first K-digit prime in consecutive digits of N. If such a number does not exist, output 404 instead. Note: the leading zeroes must also be counted as part of the K digits. For example, to find the 4-digit prime in 200236, 0023 is a solution. However the first digit 2 must not be treated as a solution 0002 since the leading zeroes are not in the original number.

Sample Input 1:

20 5
23654987725541023819

Sample Output 1:

49877

Sample Input 2:

10 3
2468024680

Sample Output 2:

404

 prime的判断, stoi的写法,substr的用法

测试点2没有通过

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;

bool isPrime(int num) {
    if(num == 0 || num == 1) return false;
	for (int i = 2; i* i <= num; i++) {
		if (num % i == 0) return false;
	}
	return true;
}
int main() {
	int l, k;
	string s, temp;
	scanf("%d %d", &l, &k);
	cin>> s;
	int left;
	int ans;
	for (left = 0; left <= l - k; left++) {
		temp = s.substr(left, k);
        ans = stoi(temp);
		if (isPrime(ans)) {
            cout<<ans;
            return 0;
		}
	}
    cout<<"404\n";
    return 0;
}



/*
20 5
23654987725541023819
*/

1153 Decode Registration Card of PAT (25分)

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
  • the 2nd - 4th digits are the test site number, ranged from 101 to 999;
  • the 5th - 10th digits give the test date, in the form of yymmdd;
  • finally the 11th - 13th digits are the testee's number, ranged from 000 to 999.

Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤10​4​​) and M (≤100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner's score (integer in [0,100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

  • Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Term will be the letter which specifies the level;
  • Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
  • Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.

Output Specification:

For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

  • for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
  • for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
  • for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt's, or in increasing order of site numbers if there is a tie of Nt.

If the result of a query is empty, simply print NA.

Sample Input:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

 我太难了,这道题虽然不难,但是细节太多了,有点应付不来。涉及到字符串和数字的大量操作,非常考验基本功。

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

const int maxn = 10010;
int n, m;

struct student {
	char num[15];
	int score;
}group[maxn];

vector<student> tmp;

bool cmp1(student v1, student v2) {
	if (v1.score != v2.score) return v1.score > v2.score;
	else {
		return strcmp(v1.num, v2.num) < 0;
	}
}
struct Site {
	int index;
	int cnt;
	Site(int _index, int _cnt) : index(_index), cnt(_cnt) {}
};
bool cmp3(Site a, Site b) {
	if (a.cnt != b.cnt) return a.cnt > b.cnt;
	else return a.index < b.index;
}
int cnt = 0;
void find(int query) {
	if (query == 1) {
		char level;
		getchar();
		scanf("%c", &level);	
		printf("Case %d: 1 %c\n", ++cnt, level);
		for (int i = 0; i < n; i++) {
			if (group[i].num[0] == level) {
				tmp.push_back(group[i]);
			}
		}
		if (tmp.empty()) {
			printf("NA\n");
			return;
		}
		
		sort(tmp.begin(), tmp.end(), cmp1);
		for (int i = 0; i < tmp.size(); i++) {
			printf("%s %d\n", tmp[i].num, tmp[i].score);
		}
		tmp.clear();
	}
	else if (query == 2) {
		int site;
		int nt = 0, ns = 0;
		scanf("%d", &site);
		printf("Case %d: 2 %d\n", ++cnt, site);
		for (int i = 0; i < n; i++) {
			int cur_site = (group[i].num[1] - '0') * 100 + (group[i].num[2] - '0') * 10 + group[i].num[3] - '0';
			if (cur_site == site) {
				nt++;
				ns += group[i].score;
			}
		}
		if (nt == 0) {
			printf("NA\n");
			return;
		}
		printf("%d %d\n", nt, ns);
	}
	else if (query == 3) {
		int rst[maxn] = {0};
		vector<Site> site;
		char date[7];
		int now_cnt = 0;
		scanf("%s", &date);
		printf("Case %d: 3 %s\n", ++cnt, date);
		for (int i = 0; i < n; i++) {
			int flag = 1;
			for (int j = 4; j <= 9; j++) {
				if (date[j - 4] != group[i].num[j]) {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				now_cnt++;
				int cur_site = (group[i].num[1] - '0') * 100 + (group[i].num[2] - '0') * 10 + group[i].num[3] - '0';
				rst[cur_site]++;
			}	
		}
		for (int i = 0; i < maxn; i++) {
			if (rst[i] != 0) {
				site.push_back(Site(i, rst[i]));
			}
		}
		if (now_cnt == 0) {
			printf("NA\n");
			return;
		}
		sort(site.begin(), site.end(), cmp3);
		for (int i = 0; i < site.size(); i++) {
			printf("%d %d\n",site[i].index, site[i].cnt);
		}
	}
}
int main() {
	cin >> n >> m;
	char num[15];
	int score;
	for (int i = 0; i < n; i++) {
		scanf("%s %d", &num, &score);
		strcpy(group[i].num, num); //字符串不能直接复制
		group[i].score = score;
	}
	int query;
	for (int i = 0; i < m; i++) {
		scanf("%d", &query);
		find(query);
	}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值