﻿ Acdream 1417 Numbers（暴力枚举） - 鸿网互联

# Acdream 1417 Numbers（暴力枚举）

Numbers
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next 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

My Code：

``````#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char str[20][20];
string s[20];
LL a[20];
int main()
{
LL n, k;
while(cin>>n>>k)
{
if(!n && !k)
break;
int p = 0;
LL tp = n;
while(tp)
{
p++;
tp /= 10;
}
tp = 1;
for(int i=0; i<p-1; i++)
tp *= 10;
int cnt = 0;
LL tmp = tp%k;
if(tmp)
tmp = tp+k-tmp;
else
tmp = tp;
if(tmp <= n)
a[cnt++] = tmp;
tp /= 10;
while(tp)
{
tmp = tp%k;
if(tmp)
tmp = tp+k-tmp;
else
tmp = tp;
if(tmp < tp*10)
a[cnt++] = tmp;
tp /= 10;
}
for(int i=0; i<cnt; i++)
{
int sum = 0;
while(a[i])
{
int tp = a[i]%10;
str[i][sum++] = tp+'0';
a[i] /= 10;
}
str[i][sum] = '\0';
}
for(int i=0; i<cnt; i++)
{
int len = strlen(str[i]);
///cout<<str[i]<<endl;
for(int j=0; j<len/2; j++)
{
char ch = str[i][len-j-1];
str[i][len-j-1] = str[i][j];
str[i][j] = ch;
}
}
for(int i=0; i<cnt; i++)
s[i] = str[i];
sort(s, s+cnt);
cout<<s[0]<<endl;
}
return 0;
}
``````

<