2019 Lanqiao Cup Provincial Competition C/C++B Group Test Question B: Year string

Question B: Year string

[Problem description]
Xiao Ming uses the letter A to correspond to the number 1, B corresponds to 2, and so on, with Z corresponds to 26. For numbers above 27, Xiao Ming uses a two-digit or longer string to correspond, for example, AA corresponds to 27, AB corresponds to 28, AZ corresponds to 52, and LQ corresponds to 329.
What is the string corresponding to 2019?
[Answer submission]
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it. The result of this question is an uppercase English string. When submitting the answer, only fill in this string, pay attention to all capitals, fill in the extra content will not be able to score.

answer

This question is actually a conversion from decimal to 26

#include <iostream>
using namespace std;

char str[27] = {
    
    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'};

int main(){
    
    
	int n;
	string ans;
	cin >> n;
	
	while(n){
    
    
		ans += str[n % 26];
		n /= 26;
	}
	
	int len = ans.length();
	for(int i=len-1; i>=0; i--)
		cout << ans[i];
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44524918/article/details/109118252