Raqamlarni harflarga aylantirish dasturi

Buni amalga oshirishga harakat qilaman: A B C D E F G H I J K L M N O P Q R S U Y U V X Y Z AA AB AC nom .... ZZ AAA AAB AAC

Men bu so'zlarni aytishga urinish qiyin, lekin tushuntirishga harakat qilaman

I tried creating a base 27 system and make A represent 1, B->2 C->3, and AA->28 The problem is that every 27 letters where I get an @ representing 0.

Bundan tashqari, 0-ni vakili qilishni va 26-tayanch tizimiga ega bo'lishni sinab ko'rdim

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+ " ");
           }
    }
}

Ushbu yozuvni chiqaradi A B C D E F G H I J K L M N O P Q R S U Y U V X Y Z A @ AA AB

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

3

5 javoblar

A dan Zgacha 26 ta harf mavjud. Sizning tizimingiz 27 emas, balki 26 tag.

Ehtimol, nima qilmoqchisiz:

  • Start x at 1, not 0. Your system is currently representing 0 as an empty string, which may be throwing you off.

  • Take the quotient and remainder modulo 26, not 27.

  • Add 65 (the ASCII value for 'A') to the remainder, not 64 (the ASCII value for '@').

2
qo'shib qo'ydi
Java ASCII emas, balki Unicode foydalanadi. 65 raqamli belgisi (ba'zi belgilar uchun ikkitadan) UTF-16 kodli birlik qiymati «A» uchun, 16 bitli raqam.
qo'shib qo'ydi muallif Tom Blodget, manba

26 ta mumkin bo'lgan harflar mavjud, shuning uchun siz % va / operatorlarini foydalanganda bo'luvchi sifatida 26 dan foydalaning.

A has to represent 1, or else the sequence would be equivalent to:

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

Biroq, hisob-kitoblar oralig'i 0 dan 25gacha bo'lishi kerak.

Sizning kodingizga kiritilgan o'zgartirishlar bilan x bilan boshlayman 1 .

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

Keyinchalik while pastadirida, uni do - while pastadir o'zgartirdim va men 1 domenni 1-26 dan 0-25 ga o'tkazish uchun quotient kodini kiriting. Shuningdek, 0 kodi 'A' bilan bog'langan bo'lishi uchun 65 ( 'A' >.

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

Chiqish:

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
1
qo'shib qo'ydi
Rahmat! Bu juda yaxshi ishladi. Men bu haha ​​haqida o'ylashim kerak edi.
qo'shib qo'ydi muallif user2994363, manba

Bu savol va javoblardan Men oddiyroq yondashuv deb o'ylayman: sonni 27 tagacha aylantiring va keyin har bir belgini tegishli harf bilan almashtiring (Pshemo'nun izohiga binoan, siz «nol» belgini belgilashingiz kerak ... Men buni < kodi> _ nolga teng bo'ladi). Agar HashMap raqamni chop qilish uchun foydalanmoqchi bo'lgan maxsus belgilarni ushlab turadi.

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 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;
    }
}
0
qo'shib qo'ydi

Create a char array containing you letter like char letters[] = new char[]{'A','B', ... }. write a simple loop like do ... while(num > 0). Mod the number with letters.length and add the resulting char to a StringBuilder. Devide the number by letters.length. When the loop finishes revers the output and you are done.

Yangilash:

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));
        }

    }

}
0
qo'shib qo'ydi

Dastlab, bir nechta javoblar allaqachon aytib o'tilganidek, 26 ta harf mavjud, shuning uchun 27 taglik emas, balki 26 tagacha tizimdan foydalaning.

Bundan tashqari, A o'rniga A kodini 0 deb o'zgartiring, shuning uchun (char) (qolgan 64 ta) ni (char) (qolgan + 65) ga o'zgartiring. Siz bajarishingiz kerak bo'lgan oxirgi narsa: quotient = (int) Math.floor (quotient/27); ni o'zgartiring, chunki A ni bosish kerak, va 0 dan kichik bo'lgan koeffitsientni to'xtating.

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+ " ");
        }
     }
}

Chiqish (chiqishni boshlashida bo'sh joy ham mavjud emasligiga e'tibor bering):

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: kodingizni to'g'ri kiriting!

0
qo'shib qo'ydi
Bu ham haha ​​ishlaydi. Rahmat
qo'shib qo'ydi muallif user2994363, manba