关闭

codeforces B Spreadsheets

标签: 数学codeforcesacm算法编程
309人阅读 评论(1) 收藏 举报
分类:

.

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
2
R23C55
BC23
output
BC23
R23C55


题意:

在表格中,有两种定位方式。

1、RxCy 型,表示第x行第y列

2、**x型,(*代表任意大写字母)表示第x行,第**列。


输入任意一种形式,输出另一种形式。x和x好对应,关键是**和y之间的转换。


把一个**转换为十进制数y时,就相当于把一个26进制数转换为十进制,不难。


把一个十进制的y转换成**时,就有问题出现了,


由于Z和26等值,按理说Z应该进位啊。但是题目中这种26进制就算满了26(Z),也不进位。


因为我们在十进制的计算中,总是满10 进1的,转化成26进制时也是满26进1.


想个方法让他不进位,但又不能改变其数值。


那就在转换过程中,若低位为0(即x%26==0),说明计算时进位了,


那我们就从高位把他拿回来,让低位用Z表示,就解决了Z不进位表示的问题了。


具体看代码


代码:

#include<stdio.h>
#include<string.h>
typedef long long ll;
	char s[1000100];
void zhuan(ll x)
{
	if(x==0)
		return;
	if(x%26==0)
		x-=26;//由于26进制不进位,下一位的进制转换之前,先减掉 
	zhuan(x/26);
	int temp=x%26+'A'-1;
	if(temp<'A')
		temp='Z';//x==26时,x%26==0,特殊情况 
	printf("%C",temp);
}
void R(char s[])
{
	int len=strlen(s);
	ll x=0,y=0;
	int count=0;
	for(int i=0;i<len;i++)
	{
		if(s[i]>='0'&&s[i]<='9')
		{
			count++;
			if(count==1)
			{
				while(s[i]>='0'&&s[i]<='9')
					x=x*10+s[i++]-'0';
				i--;
			}
			else
			{
				while(s[i]>='0'&&s[i]<='9')
					y=y*10+s[i++]-'0';
				i--;
			}
		}
	}
	//进制转换
	zhuan(y);
	printf("%lld\n",x);
}
void A(char s[])
{
	int len=strlen(s);
	ll x=0,y=0;
	for(int i=0;i<len;i++)
	{
		while(s[i]>='A'&&s[i]<='Z')
		{
			x=x*26+s[i]-'A'+1;
			i++;
		}
		while(s[i]<='9'&&s[i]>='0')
		{
			y=y*10+s[i]-'0';
			i++;
		}
	}
	printf("R%lldC%lld\n",y,x);
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",s);
		int len=strlen(s);
		int count=0;
		for(int i=0;i<len;i++)
		{
			if(s[i]>='0'&&s[i]<='9')
			{
				count++;
				while(s[i]>='0'&&s[i]<='9')i++;
				i--;
			}
		}
		if(count>1)
			R(s);
		else
			A(s);
	}
	return 0;
}




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62120次
    • 积分:2202
    • 等级:
    • 排名:第19064名
    • 原创:159篇
    • 转载:9篇
    • 译文:0篇
    • 评论:29条
    博客专栏
    最新评论