zoukankan      html  css  js  c++  java
  • codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments

    贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案。

    #include<bits/stdc++.h>
    using namespace std;
    typedef unsigned long long ull;
    const int maxbit = 19;
    ull base[maxbit], n, k;
    
    void preDeal()
    {
        base[0] = 1;
        for(int i = 1; i < maxbit; i++){
            base[i] = 10*base[i-1];
        }
    }
    
    
    void ull2str(ull x,string& s)
    {
        int st[maxbit],top = 0;
        while(x){
            st[top++] = x%10+'0';
            x /= 10;
        }
        reverse(st,st+top);
        s.assign(st,st+top);
    }
    
    void work()
    {
        priority_queue<string,vector<string>,greater<string> > q;
        int sz = maxbit-1;
        while(base[sz]>n) sz--;
        string str;
        for(int i = 0; i <= sz; i++){
            ull cur = base[i];
            ull r = cur%k;
            if(r != 0){
                cur += k-r;
            }
            if(cur <= n){
                ull2str(cur,str);
                q.push(str);
            }
        }
        printf("%s
    ",q.top().c_str());
    }
    
    int main()
    {
        freopen("numbers.in","r",stdin);
        freopen("numbers.out","w",stdout);
        preDeal();
        while(scanf("%I64u%I64u",&n,&k),n){
            work();
        }
        return 0;
    }
  • 相关阅读:
    2020年4月13日
    2021年4月12日
    梦断代码阅读笔记02
    Shell基本命令
    远程链接Linux
    Linux文档与目录结构
    VMware与Centos系统安装
    linux 第一天
    day88 Vue基础
    python 生成随机验证码
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4748635.html
Copyright © 2011-2022 走看看