ACdream 1417(Number)

题目连接:http://acdream.info/problem?pid=1417

Numbers

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
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
#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[N];
}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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值