A. Yet Another Two Integers Problem(贪心水题)Codeforces Round #667 (Div. 3)
原题链接: http://codeforces.com/contest/1409/problems
测试样例:
input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
output
0
3
2
92
87654322
9150
Note:
In the first test case of the example, you don’t need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13→23→32→42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18→10→4 (subtract 8, subtract 6).
题意: 给定两个整数 a a a和 b b b,你可以对 a a a进行加减 k k k操作,其中 ( 1 ≤ k ≤ 10 ) (1 \leq k \leq 10) (1≤k≤10),问你 a a a至少要进行多少次操作才能够变为 b b b。
解题思路: 典型的贪心水题,我们要想操作次数最少,就要使得从 a a a到 b b b的速度足够快,那么显然我们是想要让 k k k尽量大。 k k k最大为10,那么我们初始值就让 k k k为10,当 k k k不满足条件: ( b − a ) > k (b-a)>k (b−a)>k时,我们自然要减小 k k k的值,直至 ( b − a ) = 0 (b-a)=0 (b−a)=0,也就是 a = = b a==b a==b,ok,则此题易解。注意:在这个过程中我们要实时更新 ( b − a ) (b-a) (b−a)的值。
AC代码:
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int nums[11]={0,1,2,3,4,5,6,7,8,9,10};
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
int t;
while(cin>>t){
int a,b;
while(t--){
cin>>a>>b;
int temp=b-a;
int ans=0,cnt;
if(temp<0)temp=(-1)*temp;
for(int i=10;i>=1;i--){
if(temp/nums[i]){
cnt=temp/nums[i];
ans+=cnt;
temp-=cnt*nums[i];
}
}
cout<<ans<<endl;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)