【问题标题】:Program to convert numbers to letters将数字转换为字母的程序
【发布时间】:2014-11-05 22:37:07
【问题描述】:

我正在尝试创建一个执行此操作的程序:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD....ZZ AAA AAB AAC

我的处理方式很难用语言表达,但我会尝试解释

我尝试创建一个以 27 为基数的系统并使 A 代表 1、B->2 C->3 和 AA->28 问题是每 27 个字母我得到一个代表 0 的@。

我也尝试让 A 代表 0 并拥有一个以 26 为基数的系统,但是当我需要它为 AA 时,27 将是 BA

public class aaa 
{
    public static void main(String args[]) 
{
    int counter=29;
    for(int x=0;x<=counter;x++)
    {   
        int quotient, remainder;
        String result="";
        quotient=x;

        while (quotient>0)
        {   
            remainder=quotient%27;
            result = (char)(remainder+64) + result;
            quotient = (int)Math.floor(quotient/27);

        }
    System.out.print(result+ " ");
           }
    }
}

这会打印出 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A@ AA AB

我希望程序执行此操作 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC

【问题讨论】:

    标签: java base letters


    【解决方案1】:

    从 A 到 Z 有 26 个字母。您的系统是基数 26,而不是 27。

    你可能想做的是:

    • x 从 1 开始,而不是 0。您的系统当前将 0 表示为空字符串,这可能会让您失望。

    • 将商和余数取模 26,而不是 27。

    • 余数加 65('A' 的 ASCII 值),而不是 64('@' 的 ASCII 值)。

    【讨论】:

    • Java 使用 Unicode,而不是 ASCII。 65 是“A”的一个(对于某些字符,需要两个)UTF-16 代码单元值,一个 16 位数字。
    【解决方案2】:

    有 26 个可能的字母,因此当您使用 %/ 运算符时,请使用 26 作为除数。

    A 必须代表1,否则序列将等同于:

    0 1 2 ... 25 00 01 02...
    

    但是,我们需要计算范围在 0-25 之间。

    修改您的代码后,我以x 开头为1

    int counter=29;
    for(int x=1;x<=counter;x++)
    {
    

    稍后在while 循环中,我将其更改为do-while 循环,并且每次从quotient 中减去1,以将域从1-26 转移到@ 987654336@。另外,我将65 ('A') 添加到余数中,以便将0 映射到'A'

        do
        {
            remainder=(quotient - 1)%26;
            result = (char)(remainder+65) + result;
            quotient = (int)Math.floor((quotient - 1)/26);
        }
        while (quotient>0);
    

    输出:

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC
    

    【讨论】:

    • 谢谢!它工作得很好。我早该想到的哈哈。
    【解决方案3】:

    作为开始,正如已经提到的几个答案,有 26 个字母,所以使用基数 26 系统,而不是 27。

    除此之外,将 A 打印为 0,而不是 @,因此将 (char)(remainder + 64) 更改为 (char)(remainder + 65)。您需要做的最后一件事是更改quotient = (int)Math.floor(quotient/27);,因为您要打印A,在此比例下将为0,因此从中减去1并在商小于0时停止循环。

    public class HelloWorld{
    
         public static void main(String []args){
            int counter=59;
            for(int x=0; x<=counter; x++)
            {   
                int quotient, remainder;
                String result="";
                quotient=x;
    
                while (quotient >= 0)
                {
                    remainder = quotient % 26;
                    result = (char)(remainder + 65) + result;
                    quotient = (int)Math.floor(quotient/26) - 1;
                }
                System.out.print(result+ " ");
            }
         }
    }
    

    输出(注意输出开头也没有空格):

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH
    

    Ps:正确缩进你的代码!

    【讨论】:

      【解决方案4】:

      来自this question and its answers 我认为有一个更简单的方法:将数字转换为基数 27,然后用适当的字母替换每个字符(正如 Pshemo 的评论所指出的,您需要定义一个“零”字符......我'将假定_ 将代表零)。 HashMap 将保存您要用于打印数字的自定义字符。

      import java.util.HashMap;
      
      public class BaseConverter
      {
          /**
           * Converts an integer to base 27, and returns the string with the custom characters
           * defined in the map.
           */
          public static String convertToMyBase27(int a) {
              HashMap<Character, Character> m = new HashMap<>();
              m.put('0', '_'); // Or another Zero character
              m.put('1', 'A');
              m.put('2', 'B');
              m.put('3', 'C');
              m.put('4', 'D');
              m.put('5', 'E');
              m.put('6', 'F');
              m.put('7', 'G');
              m.put('8', 'H');
              m.put('9', 'I');
              m.put('a', 'J');
              m.put('b', 'K');
              m.put('c', 'L');
              m.put('d', 'M');
              m.put('e', 'N');
              m.put('f', 'O');
              m.put('g', 'P');
              m.put('h', 'Q');
              m.put('i', 'R');
              m.put('j', 'S');
              m.put('k', 'T');
              m.put('l', 'U');
              m.put('m', 'V');
              m.put('n', 'W');
              m.put('o', 'X');
              m.put('p', 'Y');
              m.put('q', 'Z');
      
              String ans = "";
              String s = Integer.toString(a, 27);
              for(char c : s.toLowerCase().toCharArray()) {
                  ans += m.get(c);
              }
              return ans;
          }
      }
      

      【讨论】:

        【解决方案5】:

        创建一个包含您的字母的字符数组,例如char letters[] = new char[]{'A','B', ... }。写一个像do ... while(num &gt; 0) 这样的简单循环。用 letters.length 修改数字并将生成的字符添加到 StringBuilder。将数字除以letters.length。当循环完成时反转输出,你就完成了。

        更新:

        public class MagicNumbers
        {
            private static final char[] LETTERS = { '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        
            private static final int LLEN = LETTERS.length;
        
            private static String toMagicString(final int num)
            {
                int temp = num < 0 ? -num : num;
        
                StringBuilder strB = new StringBuilder();
        
                do
                {
                    strB.append(LETTERS[temp % LLEN]);
                    temp /= LLEN;
                }
                while (temp > 0);
        
                if (num < 0)
                {
                    strB.append('-');
                }
        
                return strB.reverse().toString();
            }
        
        
            public static void main(final String[] args)
            {
        
                for (int i = -28; i < 29; i++)
                {
                    System.out.println(toMagicString(i));
                }
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2016-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多