打印描述整数

时间:2015-10-05 07:20:26

标签: java algorithm

想要打印一个单词的英文表示(介于0和999,999之间),例如1,234,我想打印为“一千二百三十四”。找到以下解决方案,但不确定第23行到第25行的逻辑含义。任何见解都值得赞赏。谢谢。

 1 public static String numtostring(int num) {
 2    StringBuilder sb = new StringBuilder();
 3
 4    // Count number of digits in num.
 5    int len = 1;
 6    while (Math.pow((double)10, (double)len ) < num) {
 7       len++;
 8    }
 9
10    String[] wordarr1 = {“”,”One ”, “Two ”, “Three ”, “Four ”,
11       “Five ”, “Six ”, “Seven ”, “Eight ”,”Nine ”};
12    String[] wordarr11 = {“”, “Eleven ”, “Twelve ”, “Thirteen ”,
13       “Fourteen ”, “Fifteen ”, “Sixteen ”,
14       “Seventeen ”, “Eighteen ”, “Nineteen ”};
15    String[] wordarr10 = {“”,”Ten ”, “Twenty ”, “Thirty ”, “Forty ”,
16    “Fifty ”, “Sixty ”, “Seventy ”, “Eighty ”,
17    “Ninety “};
18    String[] wordarr100 = {“”, “Hundred ”, “Thousand ”};
19    int tmp;
20    if (num == 0) {
21       sb.append(“Zero”);
22    } else {
23       if (len > 3 && len % 2 == 0) {
24          len++;
25       }
26       do {
27          // Number greater than 999
28          if (len > 3) {
29             tmp = (num / (int)Math.pow((double)10,(double)len-2));
30             // If tmp is 2 digit number and not a multiple of 10
31             if (tmp / 10 == 1 && tmp%10 != 0) {
32                sb.append(wordarr11[tmp % 10]) ;
33             } else {
34                sb.append(wordarr10[tmp / 10]);
35                sb.append(wordarr1[tmp % 10]);
36             }
37             if (tmp > 0) {
38                sb.append(wordarr100[len / 2]);
39             }
40             num = num % (int)(Math.pow((double)10,(double)len-2));
41             len = len-2;
42          } else { // Number is less than 1000
43             tmp = num / 100;
44             if (tmp != 0) {
45                sb.append(wordarr1[tmp]);
46                sb.append(wordarr100[len / 2]);
47             }
48             tmp = num % 100 ;
49             if(tmp / 10 == 1 && tmp % 10 != 0) {
50                sb.append(wordarr11[tmp % 10]) ;
51             } else {
52                sb.append(wordarr10[tmp / 10]);
53                sb.append(wordarr1[tmp % 10]);
54             }
55             len = 0;
56          }
57       } while(len > 0);
58    }
59    return sb.toString();
60 }

1 个答案:

答案 0 :(得分:1)

您正在谈论此代码段:

if (len > 3 && len % 2 == 0) {
   len++;
}

这意味着如果len大于3(显然!)并且len是even数字。

%符号用于modulo operation

维基百科关于模运算的定义是:

  

在计算中,模运算在除法后找到余数   一个数字与另一个数字(有时称为模数)。

<强>更新

关于此代码段背后的逻辑。我只会试着猜测原作者的意图。 len变量将保留数字的长度。

如果长度小于100,则作者将在} else { // Number is less than 1000块中创建所需的字符串。如果您看到更好的代码段,则永远不会使用len变量,除非他使其等于零以退出循环。

因此,对于数字&lt; 999从未使用len

现在,对于数字&gt; 1000它被使用,我想作者需要更改它,以便将其用于阵列访问。这就是使用第23至25行的原因。因此,对于数字1000到9999 len是4,对于10000到99999 len是5而对于100000到999999 len是6.我想如果不改变它,则作者无法访问所需的数组值。这是我猜测为什么使用这段代码的原因。

但是,我猜测从给定的字符串开始,该方法对0到999999之间的数字应该可以正常工作。试试这个想法:

int err = 0;
for(int i = 0;i<999999;i++) {
        try {
             numtostring(i);
        } catch (Exception e){                
            err++;
        }            
    }
System.out.println(err+" ERORRS");

这将打印900000 ERRORS。对于1000的情况和从100000到999999的所有数字的情况。这两种情况在此代码中处理不正确。我发现这整个len操作非常错误,很难遵循。我不确定作者是否有其他想法。

这是一个修订版本,它似乎适用于从0到999999的数字,我发现它更容易理解。

        public static String numtostring(int num) {
    StringBuilder sb = new StringBuilder();

    // Count number of digits in num.
    int len = String.valueOf(num).length();

    String[] wordarr1 = {"", "One ", "Two ", "Three ", "Four ",
        "Five ", "Six ", "Seven ", "Eight ", "Nine "};
    String[] wordarr11 = {"", "Eleven ", "Twelve ", "Thirteen ",
        "Fourteen ", "Fifteen ", "Sixteen ",
        "Seventeen ", "Eighteen ", "Nineteen "};
    String[] wordarr10 = {"", "Ten ", "Twenty ", "Thirty ", "Forty ",
        "Fifty ", "Sixty ", "Seventy ", "Eighty ",
        "Ninety "};
    int tmp;
    if (num == 0) {
        sb.append("Zero");
    } else if (num >= 1000000) {
        System.err.println("Numbers > 999999 are not supported!");
        System.exit(1);
    } else {
        do {
            // Number greater than 999
            if (len > 3) {
                int n = num / 1000;
                sb.append(numtostring(n)).append("Thousand ");
                num = num % 1000;
                len -= String.valueOf(n).length();
            } else { // Number is less than 1000
                tmp = num / 100;
                if (tmp != 0) {
                    sb.append(wordarr1[tmp]);
                    sb.append("Hundred ");
                }
                tmp = num % 100;
                if (tmp / 10 == 1 && tmp % 10 != 0) {
                    sb.append(wordarr11[tmp % 10]);
                } else {
                    sb.append(wordarr10[tmp / 10]);
                    sb.append(wordarr1[tmp % 10]);
                }
                len = 0;
            }
        } while (len > 0);
    }
    return sb.toString();
}

您还可以通过再次调用do-while numtostring()作为参数并将其附加到n来消除sb循环。

无论如何,在使用它之前先测试一下,以确保我没有忘记任何东西:)我希望我帮助你一点!

更新2

好的,所以我们讨论过的原始方法适用于长度<1的数字。 3.它适用于4位和5位数字,但不适用于6位数之一。

让我们看看如何使用len

   if (len > 3 && len % 2 == 0) {
      // if len is 4 it becomes 5, if it is 5 it stays as is
      // if len is 6 it becomes 7 and an exception occurs
      len++;
   }
   ...
   if (len > 3) {
         // puts the thousand part in tmp
         // so if num is 9000, len is 5 and tmp is 9 (9000/10^3)
         // if num is 99000, len is 5 and tmp is 99 (99000/10^3)
         // and if num is 999000, len is 7 and tmp is 9 instead of 999 (999000/10^5)
         tmp = (num / (int)Math.pow((double)10,(double)len-2));
         // If tmp is 2 digit number and not a multiple of 10
         // So, if tmp is 11 to 19 (num was 11000 to 19999) it enters the if
         if (tmp / 10 == 1 && tmp%10 != 0) {
            // if tmp is 11 tmp % 10 is 1 and the wordarr11[1] is eleven etc.
            sb.append(wordarr11[tmp % 10]) ;
         } else {
            // if tmp is not 11 to 19 it enters here
            // this means if tmp is 1 to 9 for num 1000 to 9999
            // if tmp is 10 for num 10000 to 10999
            // if tmp is 20 to 99 for num 20000 to 99999
            // if tmp is 100 to 999 for num 100000 to 999999

            // wordarr10 contains the dozens
            // if tmp is 10 this will be ten, if it is 20 this will be twenty etc.
            // if tmp is 1 to 9 tmp / 10 will return 0 and sb will append an empty string (wordarr10[0])
            sb.append(wordarr10[tmp / 10]);

            // wordarr1 contains the units
            // if tmp is 1 to 9 then tmp % 10 will return the tmp as it is
            // if tmp is 10 or 20 tmp % 10 will return 0 and append the empty string
            // if tmp is 23 tmp % 10 will return 3 and append the word three
            sb.append(wordarr1[tmp % 10]);
         }
         // if tmp is a positive numbers... we know it is but ok...
         // we append the word hundrend if len is 2 or three, which is impossible because we are in the if(len > 3) branch
         // if original len was 4 it have become 5 earlier so
         // if len is 5 the len / 2 is 2 and we append the word thousand
         // if len is 7 len / 2 is 3 and an out of bounds exception gets thrown
         if (tmp > 0) {
            sb.append(wordarr100[len / 2]);
         }
         // finally we remove the part of num that we have printed in order to print the rest
         // so if num is 1123 then it will become 123
         // or if it is 12123 it will become again 123 (because len is 5)
         // if len is 7 this will fail and for example 123123 will become 23123
         num = num % (int)(Math.pow((double)10,(double)len-2));
         // if len is 5 then we make it three in order to run the else branch and print the rest part
         // if len was 7 this would make it 5 and the same branch would run again which I guess is also wrong
         len = len-2;

从这两行中或多或少地创造了整个混乱:

tmp = (num / (int)Math.pow((double)10,(double)len-2));
num = num % (int)(Math.pow((double)10,(double)len-2));

他们最好使用1000代替(int)Math.pow((double)10,(double)len-2)。然后其余部分或多或少会像其他部分一样。请参阅我的第一次更新,我在修改后的代码中这样做。

最后,还有另一个问题。正如我之前所说的那样,1000再次出现例外情况。这是因为长度计数错误。

int len = 1;
while (Math.pow((double)10, (double)len ) < num) {
   len++;
}

对于1000,它将返回len = 3,但对于1001,它将返回len = 4。对于10000,它将返回len = 4,但对于10001,它将返回len = 5

如果您有特定问题,请询问:)