codeforces Gym 100338E Numbers (贪心,实现)
生活随笔
收集整理的這篇文章主要介紹了
codeforces Gym 100338E Numbers (贪心,实现)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目:http://codeforces.com/gym/100338/attachments
貪心,每次枚舉10的i次冪,除k后取余數(shù)r在用k-r補(bǔ)在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\n",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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/jerryRey/p/4748635.html
總結(jié)
以上是生活随笔為你收集整理的codeforces Gym 100338E Numbers (贪心,实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: eclipse中使用svn提交,更新代码
- 下一篇: 下滑栏的实现