Codeforces1560 D. Make a Power of Two(思维+暴力)

题意:

在这里插入图片描述

解法:
2的倍数只有log种,
因此枚举2的倍数t,计算出n变成t至少需要多少次操作即可,
计算过程很简单,从高到低遍历n的数位,用来匹配t的从高到低的数位,
能匹配则匹配,不能用就必须删掉,
如果最后还是不能匹配玩,那么需要在使用加数操作.

对计算出来的数取min就是答案.
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>temp;
int n;
int check(int x){
    stack<int>s;
    while(x){
        s.push(x%10);
        x/=10;
    }
    int ans=0;
    for(auto i:temp){
        if(s.size()&&i==s.top()){
            s.pop();
        }else{
            ans++;
        }
    }
    ans+=s.size();
    return ans;
}
void solve(){
    cin>>n;
    map<int,int>mp;
    int x=n;
    temp.clear();
    while(x){
        temp.push_back(x%10);
        x/=10;
    }
    reverse(temp.begin(),temp.end());
    int ans=1e9;
    for(int i=1;i<=1e18;i*=2){
        ans=min(ans,check(i));
    }
    cout<<ans<<endl;
}
signed main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);cin.tie(0);
    int T;cin>>T;while(T--)
        solve();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值