codeforces 1 B. Spreadsheets

http://codeforces.com/problemset/problem/1/B 题意:给出了两种表格的表示方法。要求互相转化。 思路:直接模拟即可。注意和一般的进制转化不同的是,26进制对应的是1到26而不是0到25,所以要记得处理下借位。

/* ***********************************************
Author :111qqz
Created Time :2015年12月13日 星期日 19时46分09秒
File Name :code/cf/problem/1B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
1char st[100];
2int a26[100];
3int a10[100];
 1void pre()
 2{
 3    a26[0]=1;
 4    for ( int i = 1 ;i<=5; i++)
 5    {
 6	a26[i] = a26[i-1]*26;
 7//	cout<<"i:"<<i<<" "<<a26[i]<<endl;
 8    }
 9    a10[0] = 1;
10    for ( int i = 1 ; i<=7 ; i++)
11    {
12	a10[i] = a10[i-1]*10;
13    }
14}
15void solve()
16{
17    cin>>st;
18    int len = strlen(st);
19    int p1=-1,p2=-1;
20    for ( int i = 0 ; i < len ; i++)
21    {
22	char ch = st[i];
23	if (ch>='A'&&ch<='Z')
24	{
25	    if (p1==-1)
26	    {
27		p1 = i ;
28	    }
29	    else
30	    {
31		p2 = i;
32		break;
33	    }
34	}
35    }
36    if (p2-p1==1||p2==-1)
37    {
38	int dig = 0 ;
39	int alp = 0;
40	int sum = 0 ;
41	int sum2 = 0 ;
42	for ( int i = len -1 ; i>= 0 ; i--)
43	{
44	    char ch = st[i];
45	    if (!isdigit(ch))
46	    {
47		sum += (ch-'A'+1)*a26[alp];
48		alp++;
49	    }
50	    else
51	    {
52		sum2+= (ch-'0')*a10[dig];
53		dig++;
54	    }
55	}
56	printf("R%d\n",sum2,sum);
57    }
58    else
59    {
60	int dig1 = 0 ;
61	int dig2 = 0;
62	int sum1 = 0 ;
63	int sum2 = 0 ;
64	int p = 1;
65	for ( int i = len-1 ; i>= 0 ; i-- )
66	{
67	    char ch = st[i];
68	    if (isdigit(ch))
69	    {
70	//	cout<<"ch:"<<ch<<endl;
71		if (p)
72		{
73		    sum1+=(ch-'0')*a10[dig1];
74		  //  cout<<"sum1:"<<sum1<<endl;
75		    dig1++;
76		}
77		else
78		{
79		    sum2+=(ch-'0')*a10[dig2];
80		    dig2++;
81		}
 1	    }
 2	    else
 3	    {
 4		p = 0 ;
 5	    }
 6	}
 7//	cout<<"sum1:"<<sum1<<" sum2:"<<sum2<<endl;
 8	    int cnt = 0 ;
 9	    int b[50];
10	    while(sum1)
11	    {
12		cnt++;
13		b[cnt] = sum1;
14		sum1/=26;
15//		cout<<"sum1::::"<<sum1<<endl;
16	    }
17//	    cout<<"cnt:"<<cnt<<endl;
18	    for ( int i = 1 ; i <= cnt -1 ; i ++)
19	    {
20		if (b[i]<=0)
21		{
22		    b[i]+=26;
23		    b[i+1]--;
24		}
25	    }
26	    while (b[cnt]==0) cnt--;
	    for ( int i = cnt ; i >=1 ; i--)
	    {
1	//	cout<<"b[i]:"<<b[i]<<endl;
2		cout<<char(b[i]+'A'-1);
3	    }
4	    cout<<sum2<<endl;
    }
1}
2int main()
3{
4	#ifndef  ONLINE_JUDGE 
5	freopen("code/in.txt","r",stdin);
6  #endif
1	pre();
2	int T;
3	cin>>T;
4	while (T--)
5	{
6	    solve();
7	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}