【Codeforces】 B. Make it Divisible by 25

B. Make it Divisible by 25
题意:

给你一串数字,能从中移除一些数字。问最少移除多少个数字,使得这串数字代表的数能被25整除。

思路

能被25整除的数,应该是以00、25、50、75结尾的数。在数字串中出现的形式应该如下: 应该记录:(从右往左数)
… 0 … 0 … : 第二个0, 第一个0
… 5 … 0 … : 第一个0, 0之后的第一个5
… 2 … 5 … : 第一个5, 5之后的第一个2
… 7 … 5 … : 第一个5, 5之后的第一个7

AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int t;
ll n;
int a[20];
int digit(ll i){
    int cnt = 0;
    for(int i = 0; i <= 20; i++) a[i] = 0;
    while(i){
        a[++cnt] = i % 10;
        i = i / 10;
    }
    reverse(a+1, a+1+cnt);
    return cnt;
}
int main(){
    cin >> t;
    while(t--){
        cin >> n;
        int num = digit(n);
        int first_0, second_0, first_5, second_5, second_7, second_2;
        first_0 = second_0 = first_5 = second_5 = second_7 = second_2 = 0;
        for(int i = num; i >= 1; i --) {
            //00 50 75 25
            //...0...0..
            //...5...0..
            //...7...5..
            //...2...5
            if(a[i] == 0 && (!first_0 || !second_0)){
                if(!first_0) first_0 = i;
                else second_0 = i;
            }
            else if(a[i] == 5){
                if(!first_5) first_5 = i;
                if(first_0 && !second_5) second_5 = i;
            }
            else if(a[i] == 7){
                if(first_5 && !second_7) second_7 = i;
            }
            else if(a[i] == 2){
                if(first_5 && !second_2) second_2 = i;
            }
        }
        int ans1, ans2, ans3, ans4, ans;
        ans1 = ans2 = ans3 = ans4 = 0x3f3f3f3f;
        if(first_0 && second_0) ans1 = num - first_0 + first_0 - second_0 - 1;
        if(first_0 && second_5) ans2 = num - first_0 + first_0 - second_5 - 1;
        if(first_5 && second_7) ans3 = num - first_5 + first_5 - second_7 - 1;
        if(first_5 && second_2) ans4 = num - first_5 + first_5 - second_2 - 1;
        ans = min(ans1, min(ans2, min(ans3, ans4)));
        cout << ans << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值