上海市计算机学会竞赛平台二星级挑战

上海市计算机学会竞赛平台二星级挑战,第1张

上海市计算机学会竞赛平台 | 2022 二星级挑战
      • 康托表
      • 打印K型
      • 计算天数(Python)
      • 黑色星期五
      • 分割队伍
      • 调配问题(一)
      • 阶乘的余数
      • 驼峰与蛇
      • 四方定理
      • 数根
      • IP地址
      • 最年长的人
      • 选科组合
      • 合成游戏
      • 循环节的判定
      • 中心对称数
      • 扫雷
      • 永恒的生命游戏
      • 计算GPA
      • 九宫格键盘
      • 闯关升级
      • 三倍游戏
      • 救援争先
      • 数字验证
      • 评测队列
      • 买二送一
      • 考试排名
      • 巧妙的数
      • 三倍子串
      • 没有考试的天数


此文章仅供学习交流,不得抄袭刷分!!!

康托表
#include
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
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
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 
#include 
#include 
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 
#include 
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 
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 
#include 
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 
#include 
using namespace std;
long long n, i, j, k, l;
int main() {
	cin.tie(0);
	cin >> n;
	double f = int(sqrt(n));

	for (i = 0; i <= f; ++i)
		for (j = i; j <= f; ++j)
			for (k = j; k <= f; ++k)
				for (l = k; l <= f; ++l)
					if ((i * i + j * j + k * k + l * l) == n)
						cout << i << " " << j << " " << k << " " << l << endl;

	return 0;
}
数根
#include 
using namespace std;
char c;
long long sum;
int main() {
	while (cin >> c)
		sum += c - '0';

	cout << (sum - 1) % 9 + 1;
	return 0;
}
IP地址
#include 
using namespace std;

int main() {
	for (int i = 1; i <= 4; i ++) {
		char ch;
		int num = 0;

		for (int j = 1; j <= 8; j ++) {
			cin >> ch;
			num = num * 2 + ch - '0';
		}

		cout << num;

		if (i != 4) cout << '.';
	}

	return 0;
}

最年长的人
#include
#include
using namespace std;

struct data {
	int year;
	int month;
	int day;
};

bool datass(data x, data y) {
	if (x.year == y.year) {
		if (x.month == y.month)
			return x.day < y.day;
		else
			return x.month < y.month;
	} else
		return x.year < y.year;
}

int main() {
	int n;
	struct data a[1000000];

	cin >> n;

	for (int i = 0; i < n; i++)
		scanf("%d-%d-%d", &a[i].year, &a[i].month, &a[i].day);

	sort(a, a + n, datass);
	printf("%04d-%02d-%02d", a[0].year, a[0].month, a[0].day);
	return 0;
}
选科组合
#include 
using namespace std;
int a[6], maxx = 0, mid = 0, minn = 0;
int main() {
	for (int i = 0; i < 6; ++i) {
		cin >> a[i];

		if (a[i] > maxx) {
			minn = mid;
			mid = maxx;
			maxx = a[i];
		} else if (a[i] > mid) {
			minn = mid;
			mid = a[i];
		} else if (a[i] > minn)
			minn = a[i];
	}

	cout << maxx + mid + minn;
	return 0;
}
合成游戏
#include
using namespace std;
long long n, m, ans = 0, power = 1;
int main() {
	cin.tie(0);
	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> m;
		ans += m;
	}

	while (power <= ans)
		power *= 2;

	cout << power / 2;
	return 0;
}
循环节的判定
#include
using namespace std;
int main() {
	string s, a, c = "";
	cin >> s >> a;
	int len = s.length() / a.length();

	for (int i = 0; i < len; i++)
		c += a;

	if (s == c) {
		cout << "Yes";
		return 0;
	}

	cout << "No";
	return 0;
}
中心对称数
#include
using namespace std;
string a;
char b[10];
int main()
{
	cin>>a;
	b[1]='1';
	b[8]='8';
	b[0]='0';
	b[6]='9';
	b[9]='6';
	int i,n=a.size();
	for(i=0;i<n;i++)
	{
		if(a[i]=='2'||a[i]=='3'||a[i]=='4'||a[i]=='5'||a[i]=='7')
		{
			printf("Not a strobogrammatic number");
			return 0;
		}
		else if(!(a[i]==b[a[n-i-1]-'0']))
		{
			printf("Not a strobogrammatic number");
			return 0;
		}
	}
	printf("Strobogrammatic number");
	return 0;
}
扫雷
#include 
using namespace std;

int n, m;
char a[105][105];

int search(int x, int y) {
	int ans = 0;
	for (int i = x - 1; i <= x + 1; i++)
		for (int j = y - 1; j <= y + 1; j++)
			if (a[i][j] == '*')
				ans++;
	return ans;
}

int main() {
	cin >> n >> m;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (a[i][j] == '*')
				cout << '*';
			else
				cout << search(i, j);
		}
		cout << endl;
	}
	return 0;
}
永恒的生命游戏
#include 
using namespace std;
int n, m;
char map[105][105];
int main() {
	cin.tie(0);
	cin >> n >> m;

	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			cin >> map[i][j];

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			int x = 0;

			if (map[i][j] == '*') {
				if (map[i + 1][j] == '*')
					++x;

				if (map[i + 1][j - 1] == '*')
					++x;

				if (map[i + 1][j + 1] == '*')
					++x;

				if (map[i - 1][j - 1] == '*')
					++x;

				if (map[i - 1][j + 1] == '*')
					++x;

				if (map[i][j + 1] == '*')
					++x;

				if (map[i][j - 1] == '*')
					++x;

				if (map[i - 1][j] == '*')
					++x;

				if (x < 2 || x > 3) {
					cout << "Other";
					return 0;
				}
			} else {
				if (map[i + 1][j] == '*')
					++x;

				if (map[i + 1][j - 1] == '*')
					++x;

				if (map[i + 1][j + 1] == '*')
					++x;

				if (map[i - 1][j - 1] == '*')
					++x;

				if (map[i - 1][j + 1] == '*')
					++x;

				if (map[i][j + 1] == '*')
					++x;

				if (map[i][j - 1] == '*')
					++x;

				if (map[i - 1][j] == '*')
					++x;

				if (x == 3) {
					cout << "Other";
					return 0;
				}
			}
		}
	}

	cout << "Still life";
	return 0;
}
计算GPA
#include
#include

using namespace std;

int main() {
	string gpa;
	float count = 0 , len[2] = {0, 0};
	cin >> gpa;
	len[0] = gpa.size() + 1;

	for (int i = 0; i < len[0]; ++i)
		if (gpa[i] == 'A' || gpa[i] == 'B' || gpa[i] == 'C' || gpa[i] == 'D')
			len[1]++;

	for (int i = 0; i < len[0]; ++i) {
		if (gpa[i] == 'A')
			count = count + 4;
		else if (gpa[i] == 'B')
			count = count + 3;
		else if (gpa[i] == 'C')
			count = count + 2;
		else if (gpa[i] == 'D')
			count = count + 1;
		else if (gpa[i] == '+')
			count = count + 0.3;
		else if (gpa[i] == '-')
			count = count - 0.3;
	}

	float score = count / len[1];
	printf("%.2f", score);

	return 0;
}
九宫格键盘
#include 
#include 
using namespace std;

int main() {
	string s;
	int count = 0, len;
	getline(cin, s);
	len = s.length();

	for (int i = 0; i < len; ++i) {
		if (s[i] == 'a' || s[i] == 'd' || s[i] == 'g' || s[i] == 'j' || s[i] == 'm' || s[i] == 'p' || s[i] == 't'
		        || s[i] == 'w' || s[i] == ' ')
			count++;
		else if (s[i] == 'b' || s[i] == 'e' || s[i] == 'h' || s[i] == 'k' || s[i] == 'n' || s[i] == 'q' || s[i] == 'u'
		         || s[i] == 'x')
			count += 2;
		else if (s[i] == 'c' || s[i] == 'f' || s[i] == 'i' || s[i] == 'l' || s[i] == 'o' || s[i] == 'r' || s[i] == 'v'
		         || s[i] == 'y')
			count += 3;
		else if (s[i] == 'z' || s[i] == 's')
			count += 4;
	}

	cout << count;

	return 0;
}
闯关升级
#include
#include
using namespace std;

int main() {
	int n, t, x, num_a = 0, num_b, ans, sum_a[100010], sum_b[100010];
	cin >> n >> t;

	//前缀和
	for (int i = 1; i <= n; ++i)
		cin >> x, sum_a[i] += sum_a[i - 1] + x;

	for (int i = 1; i <= n; ++i)
		cin >> x, sum_b[i] += sum_b[i - 1] + x;

	while (sum_b[num_b + 1] <= t && num_b + 1 <= n)
		num_b++;

	ans = max(ans, num_b);

	while (sum_a[num_a + 1] <= t && num_a + 1 <= n) {
		num_a++;

		while (sum_a[num_a] + sum_b[num_b] > t && num_b > 0)
			num_b--;

		ans = max(ans, num_a + num_b);
	}

	cout << ans << endl;

	return 0;
}
三倍游戏
#include 
using namespace std;
long long i, n, a[100001], c[100001], ans;
int main() {
	cin >> n;

	for (i = 1; i <= n; i++) {
		cin >> a[i];
		c[a[i] % 3]++; 	// 计算每张卡片数字对3取余的余数相同的卡片个数
	}

	cout << min(c[1], c[2]) + c[0] / 2; 		// 最高得分
	return 0;
}
救援争先
#include 
#include 
using namespace std;

int n;
const int maxn = 1010;
struct team {
	int leave, arrive, id;
} a[maxn];

bool cmp(team a, team b) {
	if (a.arrive < b.arrive)
		return true;

	if (a.arrive == b.arrive) {
		if (a.leave < b.leave)
			return true;

		if (a.leave == b.leave)
			if (a.id < b.id)
				return true;
	}

	return false;
}

int main() {
	cin >> n;

	for (int i = 1; i <= n ; ++i) {
		int h, m;
		char ch;
		cin >> h >> ch >> m;
		a[i].leave = h * 60 + m;
		cin >> h >> ch >> m;
		a[i].arrive = a[i].leave + h * 60 + m;
		a[i].id = i;
	}

	sort(a + 1, a + 1 + n, cmp);

	for (int i = 1; i <= n; ++i)
		cout << a[i].id << endl;

	return 0;
}
数字验证
#include 
using namespace std;
string s;
int cnt = 0;
int main() {
	cin >> s;

	if (s[0] == '+' || s[0] == '-') s = s.substr(1);

	if (s == ".") {
		cout << "Invalid";
		return 0;
	}

	for (int i = 0; i < s.size(); i ++) {
		if (s[i] == '.') {
			cnt ++ ;

			if (cnt > 1) {
				cout << "Invalid";
				return 0;
			}
		} else if (s[i] < '0' || s[i] > '9') {
			cout << "Invalid";
			return 0;
		}
	}

	cout << "Valid";
	return 0;
}
评测队列
#include 
using namespace std;

int a[200005], b[200005], n;
long long ans, t;
int main() {
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i] >> b[i];

	ans = t = 0;

	for (int i = 1; i <= n; i++) {
		t += a[i];
		ans = max(ans, t);
		ans += b[i];
	}

	cout << ans;
	return 0;
}

买二送一
#include
#include
#include
using namespace std;
long long sum;
int n, t;
bool cmp(int a, int b) {
	return a > b;
}
int main() {
	vector<int> v;

	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> t;
		v.push_back(t);
	}

	sort(v.begin(), v.end(), cmp);

	if (v.size() >= 3) {
		while (v.size() >= 3) {
			sum += v[0] + v[1];

			if (v.size() >= 3)
				v.erase(v.begin(), v.begin() + 3);
		}
	}

	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
		sum += *it;
	}

	cout << sum;
	return 0;
}
考试排名
#include 
#include 
using namespace std;

int n;
struct classes {
	int a[4];
	double b[4];
	int num, id;
} cls[10005];

bool cmp(classes x, classes y) {
	if (x.b[0] > y.b[0]) return true;

	if (x.b[0] == y.b[0]) {
		if (x.b[1] > y.b[1]) return true;

		if (x.b[1] == y.b[1]) {
			if (x.b[2] > y.b[2]) return true;

			if (x.b[2] == y.b[2]) {
				if (x.b[3] > y.b[3]) return true;

				if (x.b[3] == y.b[3]) {
					if (x.num > y.num) return true;

					if (x.num == y.num) {
						if (x.id < y.id) return true;
					}
				}
			}
		}
	}

	return false;
}

int main() {
	cin.tie(0);
	cin >> n;

	for (int i = 1; i <= n; i ++) {
		string s;
		cin >> s;
		int len = s.size();

		for (int j = 0; j < len; j ++) {
			int t = s[j] - 'A';
			cls[i].a[t] ++;
		}

		for (int j = 0; j < 4; j ++) {
			cls[i].b[j] = cls[i].a[j] * 1.0 / len;
		}

		cls[i].num = len;
		cls[i].id = i;
	}

	sort(cls + 1, cls + 1 + n, cmp);

	for (int i = 1; i <= n; i ++) cout << cls[i].id << ' ';

	return 0;
}
巧妙的数
#include 
using namespace std;

int main() {
	bool mt[10];
	int a[1010], n;
	char ch;

	while (cin >> ch) {
		a[++n] = ch - '0';
		mt[a[n]] = true;
	}

	for (int i = 2; i <= 9; ++i) {
		if (mt[i]) {
			int r = 0;

			for (int j = 1; j <= n; j ++) {
				r = (r * 10 + a[j]) % i;
			}

			if (r != 0) {
				cout << "not clever";
				return 0;
			}
		}
	}

	cout << "clever";

	return 0;
}
三倍子串
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {
	char str[1000005];

	while (scanf("%s", str) != EOF) {
		long long sum[3] = {0};
		int s = 0;
		int n = strlen(str);

		for (int i = 0; i < n; i++) {
			s += str[i] - '0';
			s %= 3;
			sum[s]++;
		}

		long long ans;
		ans = (sum[0] + 1) * sum[0] / 2 + sum[1] * (sum[1] - 1) / 2 + sum[2] * (sum[2] - 1) / 2;

		printf("%lld\n", ans);
	}

	return 0;
}

没有考试的天数
#include 
using namespace std;

int gcd(int x, int y) {
	if (y == 0) return x;

	return gcd(y, x % y);
}

int main() {
	int n, a, b, c;
	cin >> n >> a >> b >> c;

	int d = a / gcd(a, b) * b;
	int e = b / gcd(b, c) * c;
	int f = c / gcd(c, a) * a;

	int g = d / gcd(d, c) * c;

	cout << n - (n / a + n / b + n / c) + (n / d + n / e + n / f) - n / g;

	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/733427.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存