A - Last Two Digits
Problem Statement
You are given an integer N at least 100. Print the last two digits of N.
Strictly speaking, print the tens and ones digits of N in this order.
Constraints
100≤N≤999
Sample Input 1
254
Sample Output 1
54
The last two digits of 254 are 54, which should be printed.
Sample Input 2
101
Sample Output 2
01
The last two digits of 101 are 01, which should be printed.
代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
int main() {
string str;
cin >> str;
cout << str[1] << str[2];
return 0;
}
B - Practical Computing
Sample Input 1
3
Sample Output 1
1
1 1
1 2 1
Sample Input 2
10
Sample Output 2
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
思路
杨辉三角啊,模拟一下就好了,我可能写的太麻烦了,主对角线和最左边一列都可以放在同一个循环里的
代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
int a[31][31];
int main() {
int n;
scanf("%d", &n);
// 主对角线
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++)
a[i][j] = 1;
}
// 最左一列
for (int i = 0; i < n; i++) {
a[i][0] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++)
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0)
printf("%d", a[i][j]);
else printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
代码2
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
int a[31][31];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) a[i][j] = 1;
else
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0)
printf("%d", a[i][j]);
else printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
C - K Swap
Problem Statement
Output
If it is possible to sort A in ascending order, print Yes; otherwise, print No.
Sample Input 1
5 2
3 4 1 3 4
Sample Output 1
Yes
Sample Input 2
5 3
3 4 1 3 4
Sample Output 2
No
Sample Input 3
7 5
1 2 3 4 5 5 10
Sample Output 3
Yes
思路
给你一组元素,可以将第 i 个元素和第i + k 个元素交换,进行任意次数的交换之后,问:能否形成升序排序。
代码
比赛的时候没写出来呜呜呜,我好菜
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 1;
const int MOD = 1e9 + 7;
/*
k = 1,就是冒泡排序,显然可以排序成功
对于任意的k:
把对k取余相同的各数放在同一个数组里面,并排序,遍历1-n,
每次取相应数组里面最小的数,形成一个新的数组,如果和排序之后的原数组相等,namo可以
*/
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<int> a(n);
vector<vector<int>> b(k); // 二维数组存放数字
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i % k].push_back(a[i]);
}
// 对每一组数据进行降序排序
for (int i = 0; i < k; i++) {
sort(b[i].rbegin(), b[i].rend());
}
sort(a.begin(), a.end());
vector<int> res; // 存放结果
for (int i = 0; i < n; i++) {
res.push_back(b[i % k].back());
b[i % k].pop_back();
}
if (a == res) printf("Yes");
else printf("No");
return 0;
}