codeforces1BSpreadsheets(26进制转换)

xiaoxiao2021-02-28  81

B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard input output standard output

In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input

The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106.

Output

Write n lines, each line should contain a cell coordinates in the other numeration system.

Examples input 2R23C55BC23 output BC23R23C55

题意:

表格有两种表示某个单元格的方法,一种例如R23C55,表示第23行55列;另一种例如 BC23,表示23行(2*27+3)=55列;题目要求是实现两种形式的转换,字母均为大写。

题目看起来比较简单,就是进行一个26进制和十进制的互相转换,需要注意的是

1.A代表1,计数并不是从零开始的。

2.判断是哪种格式

下面是第一次做时AC的代码:

#include<bits/stdc++.h> using namespace std; int n,a,b; char p[]="ZABCDEFGHIJKLMNOPQRSTUVWXY"; char ans[10000]; int fun(int val){ int i=0; memset(ans,'/0',sizeof(ans)); while(val){ if(val&){ ans[i]=p[val&]; val/=26; } else{ ans[i]='Z'; val/=26; val--; } i++; } return i; } int funn(){ } int main(){ ios::sync_with_stdio(false); string s; cin>>n; while(n--) { cin>>s; if(s[0]=='R'&&s[1]>='0'&&s[1]<='9') { a=b=0; int i; for(i=1;i<s.size();i++){ if(s[i]=='C')break; a=a*10+(s[i]-'0'); } if(i==s.size()){ cout<<"R"<<a<<"C"<<'R'-'A'+1<<endl; continue; } i++; for(;i<s.size();i++){ b=b*10+(s[i]-'0'); } int len=fun(b); for(int i=len-1;i>=0;i--) cout<<ans[i]; cout<<a<<endl; } else { a=b=0; int i; for(i=0;i<s.size();i++) { if(s[i]>='0'&&s[i]<='9')break; a=a*26+s[i]-'A'+1; } for(;i<s.size();i++) { b=b*10+(s[i]-'0'); } cout<<"R"<<b<<"C"<<a<<endl; } } }

另一种写法

#include <cstdio> char s[15], c; int num(int b, int l) { int temp = 0; for (int i = b; i < l; ++i) temp = temp*10+s[i]-'0'; return temp; } int pow(int x, int y) { int temp = 1; for (int i = 0; i < y; ++i) temp *= x; return temp; } int main() { int n; scanf("%d\n",&n); while(n--) { int l = 0, f = 0; while ((c = getchar()) != '\n') { s[l++] = c; if (s[l-2] >= '0' && s[l-2] <= '9' && c > '9') f = l-1; } if (f) { int temp = num(f+1,l); l = f; while (temp > 0) { c = temp&+'@'; if (c == '@') { --temp; c = 'Z'; } s[++l] = c; temp /= 26; } for (int i = l; i > f; --i) putchar(s[i]); for (int i = 1; i < f; ++i) putchar(s[i]); putchar('\n'); } else { putchar('R'); while (s[++f] > '9'); for (int i = f; i < l; ++i) putchar(s[i]); int temp = 0; for (int i = 0; i < f; ++i) temp += (s[i]-'@') * pow(26,f-i-1); printf("C%d",temp); putchar('\n'); } } return 0; }

转载请注明原文地址: https://www.6miu.com/read-36239.html

最新回复(0)