快手面试第一题【字符串字母递进】

题目

A
AA AB AC … AZ
BA BB BC … BZ

ZA ZB ZC … ZZ
AAA …
按这个规律输出第n个数的字符串

本人C++算法

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

猜你喜欢

转载自blog.csdn.net/qq_37249793/article/details/130856522