[第十届蓝桥杯省赛C++B组]年号字串 原创

题目来源:第十届蓝桥杯省赛C++B组

算法标签:进制转换

题目描述:

小明用字母 A 对应数字 1,B 对应 2,以此类推,用 Z 对应 26。对于 27 以上的数字,小明用两位或更长位的字符串来对应,例如 AA 对应 27,AB 对 应 28,AZ 对应 52,LQ 对应 329。 请问 2019 对应的字符串是什么?

思路

二十六进制

题目代码

都存在边界问题,不过似乎够用,实在不行上Excel了

朴素
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string ans;
    int n=2019;
    while(n)
    {
        int t = n%26;
        n/=26;
        ans+=s[t-1];
    }
    for(int i=ans.size()-1;i>=0;i--)cout<<ans[i];
    return 0;
}
递归
#include<iostream>
#include<stack>

using namespace std;

stack<char> ans;

void check(int n){
	if(n)ans.push(char(n%26+'A'-1));

	if(n)n/=26;
	if(!n)return;
	check(n);
}

int main(){
	
	check(2019);		
	while(ans.size()){
		cout<<ans.top(),ans.pop();
	}
	
	cout<<endl;		

	return 0;
}

#include <iostream>
using namespace std;

void back26(int s)
{
    if(!s)return ;
    back26(s/26);
    cout<<(char)(s%26+64);
}
int main()
{
    back26(2019);
    return 0;
}
#include<iostream>

using namespace std;

void check(int n){
        if(!n)return;
        check(n/26);
        cout<<(char(n%26+'A'-1));
}

int main(){
        check(2019);
        cout<<endl;

        return 0;
}

答案

BYQ

posted @ 2024-03-31 16:11  俺叫西西弗斯  阅读(0)  评论(0)    收藏  举报  来源