题目解析:核心的部分就在于进制的转化,懒得多说,就放代码。
#include<iostream> #include<string> #include<queue> #include<cmath> #include<algorithm> #include<cstring> #define ll long long using namespace std; void change_10to26(int s) { int len[100], cnt = 0; int temp; while (s) { temp = s % 26; if (temp == 0)temp = 26;//当temp为0的时候它就是Z len[cnt++] = temp; s = (s - temp) / 26;//为啥呢,比如如果输入,应该是A但不加这条就会出错. } for (int i = cnt - 1; i >= 0; i--) cout << (char)(len[i] + 'A' - 1); } void change_26to10(string s) { int res = 0; for (int i = 0; i < s.size(); i++) { res *= 26; res += s[i] - 64; } cout << res; } int main() { int n; cin >> n; int i; while (n--) { char k[100]; char op[100]; scanf("%s", k); int a, b; if (sscanf(k, "R%dC%d", &a, &b) == 2) {//sscanf函数,如果没做到这题我是真的不知道,多学学多看看,我太菜了 change_10to26(b); cout << a << endl; } else { string s = ""; for (i = 0; k[i] >= 'A'&&k[i] <= 'Z'; i++) s += k[i]; a = 0; for (int j = i; j < strlen(k); j++) { a *= 10; a += k[j] - '0'; } cout << "R" << a << "C"; change_26to10(s); cout << endl; } } return 0; }
最后一句话,我太菜了