Question :A Defected meter is given with missing number 4
from it's rim.WAP that convert the wrong reading into right one.
Example:
input 100
output 81
hint: (41,24,34,40-49,54,64,74,84,94)is missed.
package vinsol; /* count 1,2,3,5,6,7,8,9,10============9 * 11,12,13,15,16,17,18,19,20====9 * 21,22,23,25,26,27,28,29,30===9 * 31,32,33,35,36,37,38,39======8 * 50==1 * cases--miss ---correct value * 10 ----1-----9 * 100----19-----81 * 1000---19*9+99=270---770 * 10000--270*9 +999=3429---6571 * there is pattern ..use this pattern with care. *note: so input can never be 4,44,444 */ public class DefectedMeter { static int correctMe(int num)// correct me like you do. { if (num < 4) return num; else if (num >= 4 && num <= 10) return num - 1;// like 9 int n = 10, p = 9; while(true) { if (num >= (n) && num <= (n * 3 + p)) if (num % n <= 3) return num - ((num / n) * (correctMe(n / 10)) + n * (0)); else return num - ((num / n + 1) * (correctMe(n / 10)) + n * (0)); if (num > (n * 3 + p) && num <= (n * 10)) if (num % n <= 3) return num - ((num / n - 1) * (correctMe(n / 10)) + n * (1)); else return num - ((num / n) * (correctMe(n / 10)) + n * (1)); p = n + p; n *= 10; } } public static void main(String[] args) { System.out.println(correctMe(10));// 9(4) System.out.println(correctMe(13));// 12(4) System.out.println(correctMe(15));// 13(4,14) System.out.println(correctMe(20));// 18(4,14) System.out.println(correctMe(30));// 27(4,14,24) System.out.println(correctMe(33));// 30(4,14,24) System.out.println(correctMe(35));// 31(4,14,24) System.out.println(correctMe(39));// 35(4,14,24,34) System.out.println(correctMe(50));// 36(4,14,24,34,40-49) System.out.println(correctMe(53));// 39(4,14,24,34,40-49) System.out.println(correctMe(55));// 40 System.out.println(correctMe(100));// 4,14,24,34,40-49,54,64,74,84,94 System.out.println(correctMe(1000)); System.out.println(correctMe(10000)); System.out.println(correctMe(100000)); } }
OUTPUT 9 12 13 18 27 30 31 35 36 39 40 81 819 8271 82629