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

本文涵盖了上海市计算机学会竞赛中的多个算法题目,包括康托表、打印K型、计算天数、黑色星期五等,涉及Python编程、数学问题、游戏策略等。通过这些挑战,旨在提升参赛者的算法思维和问题解决能力。
摘要由CSDN通过智能技术生成


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

康托表

#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
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dsmtyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值