题目
题意: 给定一个数或者大写字母组成的字符串,相互转换。转换规则参考了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;
}