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