ACdream 1417(Number)
2014-10-05 15:58
302 查看
题目连接:http://acdream.info/problem?pid=1417
Time Limit: 2000/1000MS (Java/Others)Memory
Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext
Problem
Consider numbers from 1 to n.
You have to find the smallest lexicographically number among them which is divisible by k.
Input file contains several test cases. Each test case consists of two integer numbers n and k on a line(1 ≤ n ≤ 1018,
1 ≤ k ≤ n).
The last test case is followed by a line that contains two zeroes. This line must not be processed.
For each test case output one integer number — the smallest lexicographically number not exceeding n which is divisible by k.
多组数据
Andrew Stankevich Contest 22
mathlover
SubmitStatistic
ACdream v1.5.3 2013-2014 KIDx(网站), TTLast(评测机)
Numbers
Time Limit: 2000/1000MS (Java/Others)MemoryLimit: 128000/64000KB (Java/Others)
SubmitStatisticNext
Problem
Problem Description
Consider numbers from 1 to n.You have to find the smallest lexicographically number among them which is divisible by k.
Input
Input file contains several test cases. Each test case consists of two integer numbers n and k on a line(1 ≤ n ≤ 1018,1 ≤ k ≤ n).
The last test case is followed by a line that contains two zeroes. This line must not be processed.
Output
For each test case output one integer number — the smallest lexicographically number not exceeding n which is divisible by k.
Sample Input
2000 17 2000 20 2000 22 0 0
Sample Output
1003 100 1012
Hint
多组数据
Source
Andrew Stankevich Contest 22
Manager
mathloverSubmitStatistic
ACdream v1.5.3 2013-2014 KIDx(网站), TTLast(评测机)
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <algorithm> typedef long long ll; const int N=25; using namespace std; struct node { char s ; }str[30]; bool cmp(node a,node b) { return strcmp(a.s,b.s)<0; } ll pow(int a,int b) { ll ans=1; for(int i=1;i<=b;i++) ans=ans*a; return ans; } int main() { ll n,k,a[30],cnt; while(scanf("%lld%lld",&n,&k)!=EOF) { if(n==0&&k==0)break; cnt = 0 ; a[cnt ++] = k ; ll kk=k,numberk=0; while(kk) { kk=kk/10; numberk++; } for(int i=numberk;i<=18;i++) { ll temp=pow(10,i); if(temp>n)break; if(temp%k==0) { a[cnt++]=temp; continue; } ll tmp=(temp/k+1)*k; if(tmp>n)break; //注意得判断一下是否大于n a[cnt++]=tmp; } /* for(int i=1;i<=18;i++) { ll tmp = pow( 10 , i ) ; if ( tmp > n ) break ; tmp = tmp + ( k - ( tmp - 1 ) % k - 1 ) ; if ( tmp <= k ) continue ; if ( tmp > n ) break ; a[cnt ++] = tmp ; } */ for(int i=0;i<cnt;i++) { sprintf(str[i].s,"%lld",a[i]); } sort(str,str+cnt,cmp); printf("%s\n",str[0].s); } return 0; }
相关文章推荐
- acdream Number theory
- ACdream - 1417Numbers(贪心构造)
- ACdream 1187 Rational Number Tree(搜索:DFS)
- ACdreamoj 1417 思维题
- ACdream 1057 Vision Field 线段树+二分查找
- ACdream 1210 Chinese Girls' Amusement 大数+思维
- ACdream 1427 Nice Sequence
- ACdream 1064 完美数(数位dp)
- 306. Additive Number 自己写的,居然ac了,写一遍比看100遍强,看别人对边界的处理纯属浪费时间,不如自己动手,自己手写才能提高
- ACdream 1427 Nice Sequence
- acdreamOJ 1236 求无向图的桥
- ACdream 1740 - 哗啦啦族的24点游戏 (全排列DFS)
- 二分法入门(二)——POJ 3258,2976;HDU 4430;CodeForces 535C;Gym 101194D;ACdream 1066
- acdream 1684(博弈)
- ACdream-1066-Bet
- acdream 1682 吃不完的糖果(环形最大子段和)
- Acdream 1084 寒假安排
- ACdreamoj(1105)模拟题
- ACdream OJ 1140 Counting Triangles
- acdream(18) Integration of Polynomial