Spreadsheets

  • 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 ≤ 10^5), 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.

Note that this question is not a simple hexadecimal conversion, because AZ corresponds to 1-26, there is no letter corresponding to 0. For example, when 26 is converted to 26, the hexadecimal is 10, but Z should be output.

#include<bits/stdc++.h>
using namespace std;
void change1(string s){
	int t=0,a=0;
	while(t<s.size()&&s[t]>='A'&&s[t]<='Z')
		a*=26,a+=s[t++]-'A'+1;
	cout<<"R";
	while(t<s.size()) cout<<s[t++];
	cout<<"C"<<a<<endl;
}
void change2(string s){
	int t=1,a=0,b=0;
	string k="";
	while(s[t]>='0'&&s[t]<='9') a*=10,a+=s[t++]-'0';
	t++;
	while(t<s.size()) b*=10,b+=s[t++]-'0';
	while(b)
		k=(char)((--b)%26+'A')+k,b/=26; 
	cout<<k<<a<<endl;
}
int main(){
	int n;
	string s;
	cin>>n;
	while(n--){
		cin>>s;
		int t=0;
		while(s[t]>='A'&&s[t]<='Z') t++;
		while(t<s.size()&&s[t]>='0'&&s[t]<='9') t++;
		if(t==s.size()) change1(s);
		else change2(s);
	} 
	return 0;
} 

 

Guess you like

Origin blog.csdn.net/u013455437/article/details/109315775