The first question of Kuaishou interview [string letter progression]

topic

A
AA AB AC ... AZ
BA BB BC ... BZ
...
ZA ZB ZC ... ZZ
AAA ...
output the string of the nth number according to this rule

My C++ algorithm

#include <iostream>
#include <string>

using namespace std;

class Letter {
    
    
private:
	string str;
	int input;
	int str_num = 0;
public:
	int GetLetter(int input) {
    
    
		this->input = input;
		while (input > 0) {
    
    
			str_num = 64 + input % 26;
			
			if (str_num == 64) {
    
    
				str = (char)90 + str;
				input -= 26;
			}
			else {
    
    
				str = (char)str_num + str;
				input -= input % 26;
			}
			input /= 26;
		}
		cout << str << endl;
		str.erase();
		return 0;
	}
};

int main() {
    
    
	Letter letter;
	int test_num;
	while (1) {
    
    
		cin >> test_num;
		letter.GetLetter(test_num);
	}
}

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/130856522