Codeforces 1B Spreadsheets
程序员文章站
2022-03-02 23:05:49
...
真是被进制转换恶心到了…
首先先判断输入的字符串是哪种类型的,然后26进制转换。字母转数字比较简单,但是数字转字母需要多考虑一下,A - Z 表示1到26的话会有很多问题(自己试一试就知道了–)所以把 A- Z 表示 0 - 25,每次sumc%26 变成(sumc-1)%26,这样得出的余数正好就是到A的“偏移量”。并且除26的时候也要考虑余数为0的特殊情况 .
#include <bits/stdc++.h>
using namespace std;
int n,f;
string s ,ans;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
while(n--){
cin>>s;
ans.clear();
int i = s.size()-1;
while(isdigit(s[i])&&i) i--;
if(isdigit(s[i-1])){
string row = s.substr(1,i-1);
string col = s.substr(i+1,s.size()-1);
int sumc = 0;f=0;
for(i = 0;i<col[i];i++) sumc = sumc *10 + col[i] - '0';
while(sumc){
int num = (sumc-1) % 26;
ans += 'A' + num ;
if(sumc % 26) sumc /= 26;
else sumc = sumc /26 -1;
}
reverse(ans.begin(),ans.end());
ans += row;
cout<<ans<<endl;
}
else {
ans += 'R';
ans += s.substr(i+1,s.size()-1);
ans += 'C';
int sumc = 0 ;
string col = s.substr(0,i+1);
for(i = 0;col[i];i++){
int num = s[i] - 'A'+1;
sumc = sumc * 26 + num;
}
cout<<ans<<sumc<<endl;
}
}
return 0;
}
推荐阅读
-
codeforces248(div1) B Nanami's Digital Board
-
Codeforces Round #214 (Div. 2)??Dima and Salad_html/css_WEB-ITnose
-
CodeForces-1461E-Water Level
-
Codeforces Round #251 (Div. 2)-C,D_html/css_WEB-ITnose
-
codeforces 977D(DFS)
-
Codeforces Round #252 (Div. 2)-C,D_html/css_WEB-ITnose
-
CodeForces 710D Two Arithmetic Progressions
-
CodeForces 1313E Concatenation with intersection
-
Codeforces Round #277.5 (Div. 2)-D_html/css_WEB-ITnose
-
Codeforces Round #225 (Div. 1) C 树状数组 || 线段树_html/css_WEB-ITnose