D. Decrease the Sum of Digits

编程入门 行业动态 更新时间:2024-10-22 19:37:36

D. <a href=https://www.elefans.com/category/jswz/34/1739387.html style=Decrease the Sum of Digits"/>

D. Decrease the Sum of Digits


思路:最开始是从前往后看,然后判当前sum+str[i]和s的情况,比如相等和>,相等还要判后面有没有0再考虑进位。写了一下发现这样十分麻烦。所以转化一下思路。

反正总共位数不超过20个,不如从后面开始每一位去进位,进完位后check一下当前每一位的和加起来是不是满足<=s的。所以预处理一下10进制位,然后模拟一下。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL p10[20],a[20];
char str[22];
LL cal(LL n)
{LL sum=0;for(LL i=1;i<=n;i++) sum+=a[i];return sum; 
}
void solve()
{memset(a,0,sizeof(a));memset(str,0,sizeof(str));LL s;cin>>str+1;cin>>s;LL n=strlen(str+1);for(LL i=1;i<=n;i++) a[n-i+1]=str[i]-'0';LL ans=0;LL sum=cal(n);if(sum<=s){cout<<0<<endl;return;}else{//进位 for(LL i=1;i<=n;i++){ans+=(10-a[i])*p10[i];a[i]=0;a[i+1]++;if(cal(n)<=s) break;}cout<<ans<<endl;}
}
int main(void)
{LL t;cin>>t;p10[1]=1;for(LL i=2;i<=20;i++) p10[i]=p10[i-1]*10;while(t--){solve();}
return 0;
}

 

更多推荐

D. Decrease the Sum of Digits

本文发布于:2024-03-23 14:41:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Decrease   Sum   Digits

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!