[Lanqiao Cup pre-exam assault] The 10th Lanqiao Cup Provincial C/C++ University Group B Test Questions B year string

  小明用字母 A 对应数字 1,B 对应 2,以此类推,用 Z 对应 26。对于 27
以上的数字,小明用两位或更长位的字符串来对应,例如 AA 对应 27,AB 对 应 28,AZ 对应 52,LQ 对应 329。
  请问 2019 对应的字符串是什么?
又是一道五分题
懒得去写代码
代码摘自亓官劼  使用excel去右拉着实厉害
答案为BYQ 
如何思考呢?
额........
这tm不就是满26进1吗
联想到二进制的满2进1
那这个问题不就是十进制转二十六进制吗
可以使用一般套路while--->取余--->除  见文末
#include<iostream>
using namespace std;
int main(){
    
    
    int a = 2019;
    string output = "";
    while(a){
    
    
        output = output + char(64+a%26);
        a /= 26;
    }
    for(int i = output.length()-1;i >=0 ;i--)
        cout<<output[i];
    return 0;
}
/*
答案为BYQ 
*/
/*
可以实现十进制转二,八,十六进制等。如果要实现十进制转26进制,也只是需要调整while循环内部(也就是说调整规则即可)
*/
#include<iostream>
#include<algorithm>
using namespace std;
char ans[105];
int main()
{
    
    
	int N,R,m,now;
	//N为要转换为十进制数,R为要转换成的进制
	cin>>N>>R;
	if(N<0){
    
    
		cout<<"-";
		N=-N;
	}
	m=0;
	while(N) {
    
    
		now=N%R;
		if(now<=9){
    
    
			ans[m++]='0'+now;
		}else{
    
    
			ans[m++]='A'+now-10;
		}
		N/=R;
	}
	if(m==0){
    
    
		cout<<0;
	}
	for(int i=m-1;i>=0;i--){
    
    
		cout<<ans[i];
	}
	cout<<endl;
	return 0;
} 

有问题可以留言交流٩(๑❛ᴗ❛๑)۶

Guess you like

Origin blog.csdn.net/kieson_uabc/article/details/109003748