博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Broken Calculator 最详细的解题报告
阅读量:5273 次
发布时间:2019-06-14

本文共 4632 字,大约阅读时间需要 15 分钟。

题目来源:

题目如下(链接有可能无法访问):

A Broken Calculator


Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB

Problem

Dave's calculator is broken. His calculator halts when put more than K kinds of number.

Dave wants to input an integer A, but if he put this number correctly the calculator might halt. Instead, he inputs the closest integer that won't halts the calculator.

Output the difference between Dave's input and the integer A.


Input

The input will be given in the following format from the Standard Input.

A K
  • On the first line, you will be given the integer A(1≦A≦1015), the integer Dave wants to input, followed by a space andK(1≦K≦10), the kinds of number his calculator can recognize.

Acheivements and Points

  • When you pass every test case where 1≦A≦100,000, you will be awarded 30 points.
  • In addition, if you pass all the rest test cases you will be awarded 70 more points.

Output

Output the minimum difference between Dave's input and the integer A in one line. Make sure to insert a line break at the end of the output.


Input Example 1

1234 2

Output Examle 1

12

In this case Dave can only use up to 2 kinds of numbers.

He will input the closest integer 1222, so the difference is 12.


Input Example 2

800000 1

Output Example 2

22223

Dave can use only 1 number, so 777777 is the closest integer.


Inout Example 3

7328495 10

Output Example 3

0

In this case Dave's calculator is not broken at all.

He can input the given integer A as is.


Input Example 4

262004 2

Output Example 4

218

The closest integer is 262222.

 

解题思路:

设A的数字长度为L,首先寻找K个数,对剩余的L-K数分别计算最大值和最小值,在计算的时候要考虑借位和进位的情况。

具体算法(java版,直接AC)

1 import java.math.BigInteger;  2 import java.util.Arrays;  3 import java.util.Scanner;  4   5 public class Main{  6       7     public int[]available;  8     public String target;  9     public int limited; 10      11     public Main(String target,int limited){ 12         this.available=new int[10]; 13         this.target=target; 14         this.limited=limited; 15     } 16     private int getIntByIndex(int index){ 17         return this.target.charAt(index)-'0'; 18     } 19      20     private BigInteger substract(String str1,String str2){ 21         BigInteger a=new BigInteger(str1); 22         BigInteger b=new BigInteger(str2); 23         if(a.compareTo(b)>0){ 24             return a.subtract(b); 25         }else{ 26             return b.subtract(a); 27         } 28     } 29     private String convertTo(int[]array){ 30         StringBuffer buffer=new StringBuffer(); 31         for(int i=0;i
0){ 58 System.out.println(diff2.toString()); 59 }else{ 60 System.out.println(diff1.toString()); 61 } 62 } 63 64 private int getValue(int[] avail,boolean less){ 65 if(less){ 66 for(int i=0;i<=9;i++){ 67 if(avail[i]>0) 68 return i; 69 } 70 }else{ 71 for(int i=9;i>=0;i--){ 72 if(avail[i]>0) 73 return i; 74 } 75 } 76 return -1; 77 } 78 79 public int[] calculateMax(int[] avail,int index){ 80 int[]result=new int[this.target.length()]; 81 int nextValue=Integer.MIN_VALUE; 82 for(int i=0;i
0&&i>this.getIntByIndex(index)){ 87 nextValue=i; 88 break; 89 } 90 } 91 if(nextValue==Integer.MIN_VALUE){ 92 nextValue=this.getIntByIndex(index-1)+1; 93 for(int j=index-1;j>=0;j--){ 94 if(result[j]==nextValue-1){ 95 result[j]=nextValue; 96 } 97 } 98 result[index-1]=nextValue; 99 avail[nextValue-1]=0;100 int min=Integer.MAX_VALUE;101 if(avail[nextValue]==0){102 avail[nextValue]=1;103 min=this.getValue(avail, true);104 }105 for(int j=index;j
=0;i--){126 if(avail[i]>0&&i
=0;j--){140 if(result[j]==nextValue+1){141 result[j]=nextValue;142 }143 }144 result[index-1]=nextValue;145 avail[nextValue+1]=0;146 int max=Integer.MIN_VALUE;147 if(avail[nextValue]==0){148 avail[nextValue]=1;149 max=this.getValue(avail, false);150 }151 for(int j=index;j

 

转载于:https://www.cnblogs.com/pinxiong/p/4071928.html

你可能感兴趣的文章
二、DBMS_JOB(用于安排和管理作业队列)
查看>>
Unity3d Hololens MR开发入门
查看>>
vue-分页搜索功能
查看>>
Redis源码剖析之主从复制
查看>>
Kafka实战系列--Kafka API使用体验
查看>>
【bzoj2770】YY的Treap 权值线段树
查看>>
[development][dpdk][hugepage] 大页内存的挂载
查看>>
【二代示波器教程】第15章 FreeRTOS操作系统版本二代示波器实现
查看>>
PHP学习笔记二十三【This】
查看>>
STM32学习之大纲
查看>>
P2010回文日期
查看>>
Python开发的10个小贴士
查看>>
bzoj:3616: War
查看>>
qrcode length overflow (1632>1056)--qrcode.js使用过程中二维码长度溢出解决办法
查看>>
我踩过的听过的那些坑
查看>>
CSS 制作3D旋转视频
查看>>
JQ全选反选
查看>>
sql语句中in和exists的区别
查看>>
python-处理文件
查看>>
eclipse6.5 自动注册代码
查看>>