#include <iostream>#include <cmath>usingnamespacestd;
bool isprime(int a) {
for (int i = 2; i <= sqrt(a) ; ++i) {
if (a % i == 0)
returnfalse;
}
returntrue;
}
int main() {
int a, b;
cin >> a >> b;
int vec[10001];
int k = 2;
int i = 0;
int t = 0;
while (t < b) {
if (isprime(k)) {
t++;
if (t >= a)
vec[++i] = k;
}
k++;
}
int temp = 0;
for (int j = 1; j <= i; j++) {
if (j % 10 == 1) {
++temp;
cout << vec[j];
continue;
}
cout << " " << vec[j];
if (++temp % 10 == 0)
cout << endl;
}
return0;
}
#include <iostream>usingnamespacestd;
int main() {
string a, b;
int m, n;
cin >> a >> m >> b >> n;
int alen = a.length();
int blen = b.length();
int countA = 0;
int countB = 0;
for (int i = 0; i < alen; ++i) {
if ((a[i] - '0') == m) {
countA++;
}
}
for (int i = 0; i < blen; ++i) {
if ((b[i] - '0') == n) {
countB++;
}
}
int ca = 0;
int cb = 0;
if (countA != 0) {
ca = m;
}
if (countB != 0) {
cb = n;
}
for (int i = 1; i < countA; ++i) {
ca = 10 * ca + m;
}
for (int i = 1; i < countB; ++i) {
cb = cb * 10 + n;
}
cout << ca + cb;
return0;
}
#include <iostream>usingnamespacestd;
int main() {
string s;
int a;
cin >> s >> a;
int len = s.length();
int t = 0;
int temp = 0;
t = (s[0] - '0') / a;
if ((t != 0 && len > 1) || len == 1) {
cout << t;
}
temp = (s[0] - '0') % a;
for (int i = 1; i < len; ++i) {
t = (temp * 10 + s[i] - '0') / a;
cout << t;
temp = (temp * 10 + s[i] - '0') % a;
}
cout << " " << temp;
return0;
}
1001. A+B Format (20)-PAT甲級真題
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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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 <iostream>usingnamespacestd;
int main() {
int a, b;
cin >> a >> b;
string s = to_string(a + b);
int len = s.length();
for (int i = 0; i < len; ++i) {
cout << s[i];
if (s[i] == '-')
continue;
if ((i + 1) % 3 == len % 3 && i != len - 1)
cout << ',';
}
return0;
}
1002. A+B for Polynomials (25)-PAT甲級真題
This time, you are supposed to find A+B where A and B are two polynomials.
Input
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
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
#include <iostream>usingnamespacestd;
int main() {
float c[1001] = {0};
int k1, k2, t;
float temp;
cin >> k1;
for (int i = 0; i < k1; ++i) {
scanf("%d %f", &t, &temp);
c[t] += temp;
}
cin >> k2;
for (int i = 0; i < k2; ++i) {
scanf("%d %f", &t, &temp);
c[t] += temp;
}
int count = 0;
for (int i = 0; i < 1001; ++i) {
if (c[i] != 0)
count++;
}
cout << count;
if (count != 0)
cout << " ";
for (int i = 1000; i >= 0; --i) {
if (c[i] != 0 && count > 0){
printf("%d %.1f", i, c[i]);
count--;
if (count >= 1)
printf(" ");
}
}
return0;
}
#include <iostream>#include <set>usingnamespacestd;
int main() {
int y, n;
cin >> y >> n;
for (int i = y; i <= 3012; ++i) {
set<int> s;
int num = i;
for (int j = 0; j < 4; ++j) {
s.insert(num % 10);
num /= 10;
}
if (s.size() == n) {
printf("%d %04d", i - y, i);
break;
}
}
return0;
}