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 −106≤a,b≤106. 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 N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤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.
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 andB
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 (≤104) 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 correspondingTerm
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 correspondingTerm
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 correspondingTerm
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; andinput
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
whereNt
is the total number of testees andNs
is their total score;- for a type 3 query, output in the format
Site Nt
whereSite
is the site number andNt
is the total number of testees atSite
. The output must be in non-increasing order ofNt
's, or in increasing order of site numbers if there is a tie ofNt
.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);
}
}