ACdream1417-Numbers
来源:互联网 发布:网络线上赌博 编辑:程序博客网 时间:2024/06/11 07:12
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.
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 172000 202000 220 0
Sample Output
10031001012
Hint
多组数据
Source
Andrew Stankevich Contest 22
Manager
mathlover
题意: 给一个数 n(n<10^18),求出字典序最小的不超过 n 的能够整除 k 的最小值
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <stack>using namespace std;#define ll long longint main(){ ll n,k,a[20]; char str[20][20]; string s[20]; while(cin>>n>>k) { if(!n) break; int x1=0,x2=0,cnt=0; ll m=k,q=n; while(q) { x1++; q/=10; } while(m) { x2++; m/=10; } ll p=1;\ a[cnt++]=k; for(int i=1; i<x2; i++) p*=10; for(int i=x2; i<x1; i++) { p*=10; if(p%k==0) a[cnt++]=p; else if(p+k-p%k<=n) a[cnt++]=p+k-p%k; } for(int i=0; i<cnt; i++) { int sum=0; while(a[i]) { int tmp=a[i]%10; str[i][sum++]=tmp+'0'; a[i]/=10; } str[i][sum] = '\0'; } for(int i=0; i<cnt; i++) { int len=strlen(str[i]); 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;}
0 0
- ACdream1417-Numbers
- Numbers
- Numbers
- Numbers
- Numbers
- Numbers
- Ugly Numbers
- Humble Numbers
- Humble Numbers
- Palindrom Numbers
- Niven Numbers
- Alien Numbers
- Alien Numbers
- Phone Numbers
- usaco_humble numbers
- Alien Numbers
- Alien Numbers
- hdu1058humble numbers
- Java虚拟机工作原理详解
- C# 键盘响应事件及键值对照表
- iOS简单直播实现(一:创建本地rtmp服务器)Mac上搭建直播服务器Nginx+rtmp
- AutoCompleteTextView的简单使用
- Android-自定义View的使用
- ACdream1417-Numbers
- 微信自动关闭内置浏览器
- 全局函数如何跨文件使用
- Log4j的使用教程
- C#学习笔记-Winform 界面笔记
- ThinkPHP框架学习之String类
- splice()方法删除数组中不连续的几项
- Cocoapods提高速度的镜像
- 安卓开发工程师必备的后台知识(Jfinal的配置和了解,VisualSVN server的配置,数据库的安装)