PHP: How to output list like this: AA, AB, AC, all the way to ZZZY, ZZZZ, ZZZZA etc

后端 未结 5 1382
夕颜
夕颜 2020-12-10 18:56

I\'m trying to write a function that\'ll convert an integer to a string like this, but I can\'t figure out the logic... :(

1 = a
5 = e
27 = aa
28 = ab
etc...         


        
5条回答
  •  执笔经年
    2020-12-10 19:23

    void convert(int number)
    {
    
            string str = "";
    
        while(number)
        {
            char ch;
            ch = (number - 1) % 26 + 65;    
            str = ch + str;
            number = (number-1) / 26;
        }
    
        cout << str << endl;
    }
    

提交回复
热议问题