#include <iostream> #include <climits> #include <queue> #include <set> #include <map> #include <algorithm> #include <cmath> #include <cstring> #include <cstdio> #include <vector> #define ll long long using namespace std; ll x1,x2,x3,x4; void pp(int y){ if(y>0){ pp((y-1)/26); cout<<(char)('A'+(y-1)%26); } return; } bool ws(string x){ int len=x.length(); if(x[1]>='A'&&x[1]<='Z')return 1; for(int i=2;i<len;i++){ if(x[i]=='C')return 0; } return 1; } void solve(){ int n; cin>>n; while (n--){ string a; cin>>a; if(ws(a)){ ll w=0,v=0; for(int i=0;i<a.length();i++){ if('A'<=a[i]&&a[i]<='Z'){ w=w*26+a[i]-'A'+1; } else{ v=v*10+a[i]-'0'; } } cout<<"R"<<v<<"C"<<w<<endl; } else{ int w=0; int t=0; for(int i=1;i<a.length();i++){ if(a[i]=='C'){ w=t; t=0; } else{ t=t*10+a[i]-'0'; } } pp(t); cout<<w<<endl; } } } int main() { #ifdef LOCAL_DEFINE freopen("input.txt", "rt", stdin); #endif solve(); #ifdef LOCAL_DEFINE cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
大神的
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
char s[100];
void f(int x){
if(x) {f((x-1)/26); putchar('A'+(x-1)%26);}
}
int main(){
// freopen("in.txt", "r", stdin);
int TC, r, c;
for(scanf("%d", &TC); TC; TC--){
scanf("%s", s);
if(sscanf(s, "%*[A-Z]%d%*[A-Z]%d", &r, &c)==2){
f(c); printf("%d\n", r);
}else{
c = 0;
for(int i=0; s[i]>='A' && s[i]<='Z'; i++){
c = c*26 + s[i]-'A'+1;
}
printf("R%dC%d\n", r, c);
}
}
return 0;
}
注意:
1.(y-1)/26 和(y-1)%26
2.选择递归输出;
3.sscanf的使用