Statistics

Problem Statement for "Neaten"

Problem Statement

We have a real number that we want to approximate with as few characters as possible. We require that either the absolute error or the relative error must be strictly less than 10^-k. The absolute error is the absolute difference between the values of the shortened version and the original. The relative error is the absolute error divided by the absolute value of the original (or is taken to be infinity if the original is 0).

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

  1. 2

    "00."

    Returns: 1

    The approximation "0" has 1 character and has an absolute error of 0.

  2. 2

    ".20050"

    Returns: 2

    The approximation ".2" has 2 characters and has an absolute error of .0005 (which is less than .01).

  3. 3

    "10000"

    Returns: 4

    The approximation "9995" has a relative error of 5/10000 (which is less than .001).

  4. 1

    "1001.2"

    Returns: 3

    //int k=1; String num = "1001.2"; //return "999", OY!

  5. 1

    "999.9"

    Returns: 3

    //int k=1; String num = "999.9"; //return "999"

  6. 4

    ".00000005"

    Returns: 1

    //int k=4; String num = ".00000005"; //return "0"

  7. 4

    "3.1127"

    Returns: 5

    //int k=4; String num = "3.1127"; //return "3.113"

  8. 1

    "6.94"

    Returns: 1

    //int k=1; String num = "6.94"; //return "7"

  9. 2

    "0.50"

    Returns: 2

    //int k=2; String num = "0.50"; //return ".5"

  10. 1

    "0.90"

    Returns: 2

    Please note that the error must be strictly less than 10-k.

  11. 1

    "0.901"

    Returns: 1

    //int k=1; String num = "0.901"; // "1"

  12. 2

    "9.99"

    Returns: 2

    //int k=2; String num = "9.99"; //"10"

  13. 1

    "9.901"

    Returns: 1

    //int k=1; String num = "9.901"; // "9"

  14. 1

    "9.9"

    Returns: 1

    int k=1; String num = "9.9"; //1 ("9"!)

  15. 1

    "0.1"

    Returns: 2

  16. 11

    "56448816012697901256320784509.3262878065666202699"

    Returns: 29

  17. 39

    "80824223.906420552848391407879007597553"

    Returns: 39

  18. 15

    "1"

    Returns: 1

  19. 50

    "10"

    Returns: 2

  20. 27

    "9838703736.3230328019229953904988042346"

    Returns: 27

  21. 31

    "0960828690517007475588284045408.908248553328746394"

    Returns: 30

  22. 5

    "9100.909"

    Returns: 4

    "9451.909"

  23. 3

    "91.909"

    Returns: 2

  24. 39

    "931101037958526350910346276333224208.399085936445"

    Returns: 38

  25. 19

    "93961626046241.2552907105"

    Returns: 19

  26. 5

    "9966.908336982805696188561013455342977631091645699"

    Returns: 4

  27. 1

    "11.0348749902963"

    Returns: 2

  28. 1

    "11"

    Returns: 2

  29. 8

    "976.355790815800"

    Returns: 8

  30. 1

    "104299.64654124292847088820"

    Returns: 5

  31. 1

    "999"

    Returns: 3

  32. 7

    "0.000099999"

    Returns: 5

  33. 50

    "1.00000000000000000000000000000000000000000000002"

    Returns: 49

  34. 45

    ".9999899999999999999999999999989999999999999999999"

    Returns: 31

  35. 41

    "8192857192875198712598751215161261223124125112.612"

    Returns: 46

  36. 4

    "999.99"

    Returns: 4

  37. 4

    "000784625.1432814"

    Returns: 6

  38. 50

    "1012312312.212312312312313213100000000000000000001"

    Returns: 50

  39. 38

    "129487192419287.298572394572934572349582345234523"

    Returns: 40

  40. 37

    "00120012314123412545234523450.12345001203412034"

    Returns: 39


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: