A.Star—签到

题意:判断当前的数到最近的能被100整除的数的差

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int x;
while (cin >> x) {
cout << 100 - x % 100 << endl;
}
// system("pause");
return 0;
}

B.uNrEaDaBlE sTrInG—签到

题意:一个字符串下标从 1 开始。如果奇数位都是小写英文字母,偶数位都是大写英文字母,那么输出 Yes,否则输出 No

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main() {
ios::sync_with_stdio(false), cin.tie(0);
string s;
while (cin >> s) {
bool flag = 1;
for (int i = 0; i < s.length(); ++i) {
if (i % 2 == 0 && s[i] >= 'a' && s[i] <= 'z')
continue;
if (i % 2 != 0 && s[i] >= 'A' && s[i] <= 'Z')
continue;
else {
flag = 0;
break;
}
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// system("pause");
return 0;
}

C.KaprekarNumber—字符串操作

题意:定义一个递推式,ai+1 = f(ai) (i >=0 ),每次函数的操作是对数自身和自身颠倒后的数做差并取绝对值,现在给出 a0,求出 ak 的值

思路:用字符串存便于颠倒,然后借助 atoi() 将字符串转换为 int 型做计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//string和int相互转换:https://www.cnblogs.com/smile233/p/8379802.html
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main() {
ios::sync_with_stdio(false), cin.tie(0);
string n;
int k;
while (cin >> n >> k) {
int len = n.length();
while (k--) {
sort(n.begin(), n.end());
string temp = n;
reverse(temp.begin(), temp.end());
int sum1=atoi(temp.c_str()),sum2=atoi(n.c_str());
n = to_string(sum1-sum2);
}
cout << n << endl;
}
system("pause");
return 0;
}

D.Base n—二分+溢出判断

题意:给一个大数 X(只由 0~9 组成),让 d 是 X 每位数中最大的数,有多少种进制(从 d+1 进制开始) 的 X 转换为十进制后小于等于 M

思路

  • 对于 X 是 1 的时候答案只能是 1 或者 0,
  • 对于其他的 X 因为 M 很大暴力肯定超时,就用二分找符合条件的最大进制并计算答案。但是要注意的是由于大数计算会爆 long long,所以在判断过程中如果没爆 long long 就将 X 转换为十进制和 M 比较,否则在转换过程中爆 long long 一定不符合直接退出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

char x[65];
ll m;

bool isAddOverflow(ll x, ll y) { //加法溢出判断
return LLONG_MAX - y < x;
}

bool isMulOverflow(ll x, ll y) { //乘法溢出判断y不能为0
return LLONG_MAX / y < x;
}

bool check(char *n, ll base) {
ll sum = 0, shu = 1;
for (ll i = strlen(n) - 1; i >= 0; --i) {
if (isMulOverflow(n[i] - '0', shu) || isAddOverflow((n[i] - '0') * shu, sum)) return 0; //此次转换后结果爆 long long
sum += (n[i] - '0') * shu;
if (i - 1 >= 0 && isMulOverflow(shu, base)) return 0; //转换未结束但是下次的base爆longlong
shu *= base;
if (sum > m) return 0;
}
return 1;
}

signed main() {
ios::sync_with_stdio(false), cin.tie(0);
while (cin >> x >> m) {
ll base = *max_element(x, x + strlen(x)) - '0';
if (strlen(x) == 1) {
if (base > m)
cout << 0 << endl;
else
cout << 1 << endl;
continue;
}
ll l = base + 1, r = 1+1e18; //小数+大数,否则浮点数溢出会丢弃1
while (l < r) {
ll mid = (l + r) >> 1;
if (check(x, mid))
l = mid + 1;
else
r = mid;
}
cout << (l - 1) - base << endl; //l-1是找到的最后一个符合的进制
}
// system("pause");
return 0;
}