ICL2019E

本文介绍了一个编码挑战问题的解决方案,通过贪心算法计算将两个正整数变为0所需的最少操作步骤。操作包括同时减1或使其中一个数翻倍。

https://www.codechef.com/ICL2019/problems/ICL1906

 两个整数,[0,1e5]
操作1是让两个数同时减1(只有都大于0的时候才可以用)
操作2可以让一个数乘2
问让两个数都变成0的最小操作次数 
 
直接贪心。能乘就乘。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int t,x,y;
 4 int main(){
 5     ios::sync_with_stdio(false);
 6     cin>>t;
 7     while (t--){
 8         cin>>x>>y;
 9         if(x==0&&y==0){
10             cout<<0<<endl;
11             continue;
12         }
13         if(x==0||y==0){
14             cout<<-1<<endl;
15             continue;
16         }
17         if(x>y)swap(x,y);
18         int ans = 0;
19         while (x!=y){
20             if(x*2<=y){
21                 x*=2;
22                 ans++;
23             }else{
24                 y--,x--;
25                 ans++;
26             }
27         }
28         ans+=x;
29         cout<<ans<<endl;
30     }
31 }
View Code

 

转载于:https://www.cnblogs.com/MXang/p/10624773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值