程序设计天梯赛选拔 Excel列名和列序号转换 (15 分)(什么进制转换)

题目
题意: 给定一个数或者大写字母组成的字符串,相互转换。转换规则参考了Excel表,A-Z为1-26,AA、AB、AZ为27,28,29…
思路: 十进制和26进制互相转换就行。但是注意10进制转26进制要记得每次除之前要–,因为A实际对应1,你需要用%的余数+‘A’-1,如果余数是0呢,就会出现非大写字母,那肯定寄了。反正感觉怪怪的。
时间复杂度: O(n)
代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef pair<int,int> PII;
int n,m,k,T;
template<typename T>void write(T &x)
{
	if(x<0)
	{
		putchar('-');
		x *= -1;
	}
	if(x > 9) write(x/10);
	putchar('0'+x%10);
}
template<typename T>void read(T &x)
{
	char ch = getchar(); T f = 1; x = 0;
	while(!isdigit(ch)) {if(ch=='-') f*=-1; ch = getchar();}
	while(isdigit(ch)) {x = x*10 + ch-48; ch = getchar();}
	x *= f;
}
void solve()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	string s; 
	while(cin>>s,s!="#")
	{
	if(s[0]>='0'&&s[0]<='9')
	{
		int x = stoi(s);
	    string t;
	    while(x!=0)
	    {
	    	x--;
	    	int tmp = x % 26;
	        t += char(tmp + 'A');
	        x /= 26;
	    }
	    reverse(t.begin(),t.end());
	    cout<<t<<endl;
	}
	else
	{
		int x = 0;
		for(int i=0;i<s.size();++i)
		{
			x = x * 26 + (s[i]-'A'+1);
		}
		cout<<x<<endl;
	}
	}
}
signed main(void)
{
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值