您的位置:首页 > 其它

ACdream 1417(Number)

2014-10-05 15:58 302 查看
题目连接:http://acdream.info/problem?pid=1417


Numbers

Time Limit: 2000/1000MS (Java/Others)Memory
Limit: 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

mathlover
SubmitStatistic
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: