上海市计算机学会竞赛平台 | 2022 二星级挑战
此文章仅供学习交流,不得抄袭刷分!!!
康托表
#include<iostream>
using namespace std;
int a, b;
int main() {
cin >> a >> b;
cout << (1 + a + b - 1 - 1)*(a + b - 1 - 1) / 2 + ((a + b) % 2 ? a : b);
return 0;
}
打印K型
#include<iostream>
using namespace std;
int n;
int main() {
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "**";
for (int j = 0; j < n - i; ++j)
cout << " ";
for (int j = 0; j < n - i; ++j)
cout << "*";
cout << endl;
}
cout << "***" << endl;
for (int i = n - i - 1; i >= 0; --i) {
cout << "**";
for (int j = 0; j < n - i; ++j)
cout << " ";
for (int j = 0; j < n - i; ++j)
cout << "*";
cout << endl;
}
return 0;
}
计算天数(Python)
sum = 0
year, month, day = input().split('-')
year = int(year)
month = int(month)
day = int(day)
dayNum = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
dayNum[3] += 1
for i in range(month):
sum += int(dayNum[i])
print(sum + day)
黑色星期五
#include<iostream>
using namespace std;
int days[13] = {
0, 12, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
int y, w, cnt;
int main() {
cin >> y >> w;
cnt = w;
if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
days[3]++;
for (int i = 1; i <= 12; i++) {
cnt = (cnt + days[i]) % 7;
if (cnt == 5)
cout << i << endl;
}
return 0;
}
分割队伍
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
int n, a[100001], s[100001], sum, minn = INT_MAX;
int main() {
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
sum += a[i];
}
for (int i = 1; i <= n - 1; i ++)
minn = min(minn, abs(sum - s[i] - s[i]));
cout << minn;
return 0;
}
调配问题(一)
#include <cmath>
#include <iostream>
using namespace std;
int n;
long long s = 0, ans = 0, a;
int main() {
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a;
s += a;
ans += abs(s);
}
cout << ans;
return 0;
}
阶乘的余数
#include <iostream>
using namespace std;
long long n, m, sum = 1;
int main() {
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
sum *= i;
sum %= m;
}
cout << sum % m;
return 0;
}
驼峰与蛇
#include <iostream>
#include <cstring>
using namespace std;
char name[1000001];
int main() {
int i, len;
cin >> name;
len = strlen(name);
for (i = 0; i < len; ++i) {
if (name[i + 1] >= 'A' && name[i + 1] <= 'Z' && (name[i] >= 'A' && name[i] <= 'Z')) {
name[i] = tolower(name[i]);
cout << name[i] << "_";
} else if (name[i + 1] >= 'A' && name[i + 1] <= 'Z')
cout << name[i] << "_";
else if (name[i] >= 'A' && name[i] <= 'Z') {
name[i] = tolower(name[i]);
cout << name[i];
} else {
cout << name[i];
}
}
return 0;
}
四方定理
#include <iostream>
#include <cmath>
using namespace std