• 题意:给你一个正整数\(n\),每次可以对\(n\)加一,问最少操作多少次是的\(n\)的所有位数之和不大于\(s\).

  • 题解:\(n\)的某个位置上的数进位,意味这后面的位置都可以被更新为\(0\),所以我们从高位往低位记录一个\(sum\),然后根据情况判断即可.

  • 代码:

    int t;
    int s;
    ll n;
    char str[N]; int main() {
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    t=read();
    while(t--){
    scanf("%s",str+1);
    s=read(); int len=strlen(str+1);
    int cnt=0;
    int pos=-1;
    ll n=0;
    for(int i=1;i<=len;++i){
    ll now=str[i]-'0';
    n=n*10+now;
    }
    for(int i=1;i<=len;++i){
    cnt+=str[i]-'0';
    if(str[i]=='9') continue;
    if(cnt<s) pos=i;
    } if(cnt<=s){
    puts("0");
    continue;
    } if(pos==-1){
    str[1]='1';
    for(int i=2;i<=len+1;++i) str[i]='0';
    ll tmp=0;
    for(int i=1;i<=len+1;++i){
    ll now=str[i]-'0';
    tmp=tmp*10+now;
    }
    printf("%lld\n",tmp-n);
    continue;
    } str[pos]++;
    for(int i=pos+1;i<=len;++i) str[i]='0';
    ll tmp=0;
    for(int i=1;i<=len;++i){
    ll now=str[i]-'0';
    tmp=tmp*10+now;
    }
    printf("%lld\n",tmp-n);
    } return 0;
    }

Codeforces Round #667 (Div. 3) D. Decrease the Sum of Digits (贪心)的更多相关文章

  1. Codeforces Round #667 (Div. 3)

    比赛链接:https://codeforces.com/contest/1409 A. Yet Another Two Integers Problem 题意 给出两个数 $a$ 和 $b$,有以下两 ...

  2. Codeforces Round #667 (Div. 3) B、C、D、E 题解

    抱歉B.C题咕了这么久 B. Minimum Product #枚举 #贪心 题目链接 题意 给定四个整数\(a, b, x, y\),其中\(a\geq x, b\geq y\),你可以执行不超过\ ...

  3. Codeforces Round #667 (Div. 3) E. Two Platforms (双指针)

    题意:有\(n\)个点往下落,你可以在最下面放两个长度为\(k\)的板子,问做多能接到多少个点. 题解:这题给纵坐标\(y\)完全没有用,我们先对横坐标\(x\)排序,然后从左边开始枚举,用\(l[i ...

  4. Codeforces Round #667 (Div. 3) C. Yet Another Array Restoration (数学)

    题意:给你两个数字\(x\)和\(y\),让你构造一个长度为\(n\)的序列,要求包含\(x\)和\(y\),并且排序后相邻两项的差值相等. 题解:有排序后相邻两项的差值相等可知,构造的序列排序后一定 ...

  5. Codeforces Round #667 (Div. 3) B. Minimum Product (贪心,数学)

    题意:给你\(a\)和\(b\)两个数,每次操作可以是任意一个数\(-1\),最多操作\(n\),并且\(a\ge x\),\(b\ge y\),求操作后\(a*b\)的最小值. 题解:观察样例并且在 ...

  6. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  7. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  8. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  9. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

    A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

随机推荐

  1. mongodb表索引备份,索引的导出导入

    背景 发现有两个mongodb环境的数据库表索引不一致,另一个数据库有索引缺失,需要将一个数据库里的所有表索引导入到另一个数据库 也可用于单独备份数据库所有表的索引 写mongo shell的js脚本 ...

  2. 十五:SQL注入之oracle,Mangodb注入

    Access,Mysql,mssql,mangoDB,postgresql,sqlite,oracle,sybase JSON类型的数据注入: 键名:键值 {"a":"1 ...

  3. MySQL的索引优化分析(二)

    一.索引优化 1,单表索引优化 建表 CREATE TABLE IF NOT EXISTS article( id INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO ...

  4. ./utils/build.sh: line 131: patch: command not found

    安装 percona-xtrabackup-2.1.5过程中遇到如下问题: [root@test percona-xtrabackup-2.1.5]# ./utils/build.sh innodb5 ...

  5. ctfhub技能树—文件上传—MIME绕过

    什么是MIME MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访 ...

  6. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp

    [root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf root@192.168.30.23:/etc/sysctl.d/k8s.conf@@@@@@@@@@@ ...

  7. React中的合成事件

    React中的合成事件 React自己实现了一套高效的事件注册.存储.分发和重用逻辑,在DOM事件体系基础上做了很大改进,减少了内存消耗,简化了事件逻辑,并最大程度地解决了IE等浏览器的不兼容问题. ...

  8. 09--Docker 安装tomcat9

    1.在hub.docker.com中获取tomcat拉取地址 docker pull tomcat:9.0.41-jdk8-corretto 2.查看Dockerfile 中WORKDIR 为/use ...

  9. SpringMVC下关于静态资源访问

    SpringMVC静态资源访问 聊一聊关于静态资源的访问问题 首先,我们要对web.xml里面的DispatcherServlet进行设置 <!-- 中央调度器--> <servle ...

  10. 二本学生拿到腾讯大厂offer的成长记录

    本人迈莫,是在20年以春招实习生的身份进入鹅厂,经过重重波折,最终成为鹅仔一份子.接下来我会以我亲生经历为例,分享一下普通大学的学生也是可以进去大厂,拭目以待!!! 初入大学 惨遭毒打 时间倒回到17 ...