Codeforces Round #748 (Div. 3) B. Make it Divisible by 25
作者:互联网
B. Make it Divisible by 25
题目大意:
给出一个数,你可以删除任意数位,问最少删除多少次才能使删除后的数能够被25整除
思路:
能被25整除的数的性质:最后的两位数必须是00、 25 、50、75这几个数
然后就是
模拟模拟再模拟,细心细心再细心
AC代码:
#include <bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 1 << 30;
inline void swap(int& x, int& y) { x ^= y ^= x ^= y; }
inline int gcd(int x, int b) { return !b ? x : gcd(b, x % b); }
void solove(string x) {
int ans = INF;
int sum = 0;
for (int i = x.size() - 1; i >= 0; i--) {
if (x[i] == '5') {
bool flag = false;
int cnt;
for (int j = i - 1; j >= 0; j--) {
if (x[j] == '2' || x[j] == '7') {
cnt = i - j - 1;
sum += cnt;
flag = true;
break;
}
}
if (flag) {
ans = min(sum, ans);
sum -= cnt;
}
sum++;
}
else if (x[i] == '0') {
bool flag = false;
int cnt;
for (int j = i - 1; j >= 0; j--) {
if (x[j] == '0' || x[j] == '5') {
cnt = i - j - 1;
sum += cnt;
flag = true;
break;
}
}
if (flag) {
ans = min(ans, sum);
sum -= cnt;
}
sum++;
}
else {
sum++;
}
}
printf("%d\n", min(sum, ans));
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
string x;
cin >> x;
if (x.size() == 2) {
if (x == "25" || x == "50" || x == "75") {
printf("0\n");
}
else printf("%d\n", x[1] == '0' ? 1 : 2);
}
else {
solove(x);
}
}
return 0;
}
标签:25,cnt,748,Make,int,flag,ans,sum 来源: https://blog.csdn.net/qq_42671102/article/details/120770316