Problem Statement
We want the shortened version expressed as a string of digits, possibly with a decimal point. The original number is given to us in that form. Given k and number, return the number of characters in the shortest approximation.
Definition
- Class:
- Neaten
- Method:
- shortest
- Parameters:
- int, String
- Returns:
- int
- Method signature:
- int shortest(int k, String number)
- (be sure your method is public)
Constraints
- k will be between 1 and 50, inclusive.
- number will contain between 1 and 50 characters, inclusive.
- number will contain at most one decimal point ('.').
- Other than '.', number will contain only digits ('0'-'9').
- number will contain at least one digit.
Examples
2
"00."
Returns: 1
The approximation "0" has 1 character and has an absolute error of 0.
2
".20050"
Returns: 2
The approximation ".2" has 2 characters and has an absolute error of .0005 (which is less than .01).
3
"10000"
Returns: 4
The approximation "9995" has a relative error of 5/10000 (which is less than .001).
1
"1001.2"
Returns: 3
//int k=1; String num = "1001.2"; //return "999", OY!
1
"999.9"
Returns: 3
//int k=1; String num = "999.9"; //return "999"
4
".00000005"
Returns: 1
//int k=4; String num = ".00000005"; //return "0"
4
"3.1127"
Returns: 5
//int k=4; String num = "3.1127"; //return "3.113"
1
"6.94"
Returns: 1
//int k=1; String num = "6.94"; //return "7"
2
"0.50"
Returns: 2
//int k=2; String num = "0.50"; //return ".5"
1
"0.90"
Returns: 2
Please note that the error must be strictly less than 10-k.
1
"0.901"
Returns: 1
//int k=1; String num = "0.901"; // "1"
2
"9.99"
Returns: 2
//int k=2; String num = "9.99"; //"10"
1
"9.901"
Returns: 1
//int k=1; String num = "9.901"; // "9"
1
"9.9"
Returns: 1
int k=1; String num = "9.9"; //1 ("9"!)
1
"0.1"
Returns: 2
11
"56448816012697901256320784509.3262878065666202699"
Returns: 29
39
"80824223.906420552848391407879007597553"
Returns: 39
15
"1"
Returns: 1
50
"10"
Returns: 2
27
"9838703736.3230328019229953904988042346"
Returns: 27
31
"0960828690517007475588284045408.908248553328746394"
Returns: 30
5
"9100.909"
Returns: 4
"9451.909"
3
"91.909"
Returns: 2
39
"931101037958526350910346276333224208.399085936445"
Returns: 38
19
"93961626046241.2552907105"
Returns: 19
5
"9966.908336982805696188561013455342977631091645699"
Returns: 4
1
"11.0348749902963"
Returns: 2
1
"11"
Returns: 2
8
"976.355790815800"
Returns: 8
1
"104299.64654124292847088820"
Returns: 5
1
"999"
Returns: 3
7
"0.000099999"
Returns: 5
50
"1.00000000000000000000000000000000000000000000002"
Returns: 49
45
".9999899999999999999999999999989999999999999999999"
Returns: 31
41
"8192857192875198712598751215161261223124125112.612"
Returns: 46
4
"999.99"
Returns: 4
4
"000784625.1432814"
Returns: 6
50
"1012312312.212312312312313213100000000000000000001"
Returns: 50
38
"129487192419287.298572394572934572349582345234523"
Returns: 40
37
"00120012314123412545234523450.12345001203412034"
Returns: 39