森珀编程KOW周王争霸赛题目分享及代码

T1 三次⽅
使⽤循环找到第⼀个 3 次⽅⼤于 n 的数作为 r ,然后 r-1 ⼀定是最⼤ 3 次⽅⼩于等于 n 的数,注意要开 long long
#include <bits/stdc++.h>
using namespace std;
int main() {
	long long n;
	cin >> n;
	for (int i = 1;; i++) {
	if (1LL * i * i * i > n) {
		cout << i - 1 << " " << i << "\n";
		break;
		}
	}
	return 0;
}
T2 ⼤数比⼤⼩
当我们整数比较⼤的时候,我们需要⽤字符串来模拟⼤整数来比较⼤⼩,如果两个字符串完全相同那么就是
same ,如果不同,先根据正负来比较⼤⼩,如果符号位相同,则更具字符串⻓短比较⼤⼩,如果⻓短也相同则
根据字典序比较⼤⼩。
#include <bits/stdc++.h>
using namespace std;
int main() {
 	string a, b;
 	cin >> a >> b;
 	if (a == b)
 	cout << "same"; 
 	else if (a[0] == '-' && b[0] != '-')
 	cout << "second"; 
	else if (a[0] != '-' && b[0] == '-')
 	cout << "first"; 
 	else if (a[0] != '-' && b[0] != '-') {
 		if (a.length() < b.length() || (a.length() == b.length() && a < b))
 		cout << "second";
 		else
 		cout << "first";
 	}
 	else {
 		if (a.length() < b.length() || (a.length() == b.length() && a < b))
 		cout << "first";
 		else
 	cout << "second";
  	}
 	return 0; 
}
T3 梦中吃桃
⾸先如果 b 不等于 0 的桃⼀定可以吃到,绝对不亏,因此先把 b 不等于 0 的桃全部吃掉,然后根据累计的 b 的和,
来看看还能吃多少 b 0 的桃,因此接下来肯定由⼤到⼩吃 b 0 的桃。
#include <bits/stdc++.h>
using namespace std;
int n, a[1005], b[1005], ans, cnt, happy[1005], num;
int main() {
 	cin >> n;
 	for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
 	ans = 0, cnt = 1, num = 0;
 	for (int i = 1; i <= n; i++) {
 		if (b[i] == 0) {
 			num++;
 			happy[num] = a[i];
 		}
 		else {
 			ans += a[i];
 			cnt += b[i] - 1;
 		}
 	}
 	sort(happy + 1, happy + num + 1, greater<int>());
 	for (int i = 1; i <= min(cnt, num); i++) ans += happy[i];
 	cout << ans << "\n";
 	return 0; 
	}
T4 相似字符串
比较两个字符串是否相似,⾸先⻓度相差超过 1 的⼀定不相似,只有⻓度相差为 1 的或者⻓度相同的才可能相
似。 对于⻓度相同只需要挨个比较,不同字符相差超过 1 个的⼀定不相似。对于⻓度相差 1 的,只需要按顺序比
较,只有⻓的那个字符串除掉多的那 1 个字符,其他字符和短的字符按顺序相同,才相似,否则不相似。 不管
那个⻓的字符是 A 还是 B ⽆所谓,因此⻓度的 A 就删除那个多的字符,如果⻓的是 B 就在 A 中插入那个多的字符,
都能变⻓⼀样的字符,因此相似。
#include <iostream>
#include <string>
using namespace std;
bool isSimilar (string A, string B) {
	int m = A.size(), n = B.size();
	if (abs(m - n) > 1) return false;
	if (m == n) {
		int diff = 0;
		for (int i = 0; i < m; ++i) {
			if (A[i] != B[i]) {
				if (++diff > 1) return false;
			}
 		}
 		return diff <= 1;
 	} else {
 		string shorter = (m < n) ? A : B;
 		string longer = (m < n) ? B : A;
 		int i = 0, j = 0;
 		int diff = 0;
 		while (i < shorter.size() && j < longer.size()) {
 			if (shorter[i] != longer[j]) {
 				if (++diff > 1) return false;
 				++j;
 			} else {
 				++i;
 				++j;
 			}
 		}
 		return true;
 	}
}
int main() {
 	int T;
 	cin >> T;
 	while (T--) {
 		string A, B;
 		cin >> A >> B;
 		if (isSimilar(A, B)) 
 			cout << "similar" << endl;
		else
 			cout << "not similar" << endl;
	}
 	return 0;
}

上述所有题目均来自登录 - 徐州市信息学奥赛OJ

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值