poj 2273 An Excel-lent Problem(进制转换)

http://poj.org/problem?id=2273

行直接输出,列转化为A...Z AA....ZZ AAA...

例如AAA对应十进制数为703, 703%26 = 1(对应最后一个A),(703-1)/ 26 = 27.

27%26 = 1(对应第二个A),(27-1)/26 = 1.

1%26 = 1(对应第一个A). 即连续取余取整。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char s[30],s1[30];
int pow[7] = {0,26,702,18278,475254,12356630,321272406};

int main()
{
	while(~scanf("%s",s))
	{
		if(strcmp(s,"R0C0") == 0) break;
		int cnt = 0,i,j;
		for(i = 1; s[i]; i++)
		{
			if(s[i] == 'C') break;
			s1[cnt++] = s[i];
		}
		s1[cnt] = '\0';

		int ans = 0;
		for(j = i+1; s[j]; j++)
			ans = ans*10 + s[j]-'0';

		char tmp[10];

		if(ans <= 26)
			printf("%c",'A'+ans-1);

		else if(ans <= 702)
		{
			for(cnt = 0; cnt < 2; cnt++)
			{
				int t = ans%26;
				if(t == 0)
				{
					tmp[cnt] = 'Z';
					ans -= 26;
				}
				else
				{
					tmp[cnt] = 'A'+t-1;
					ans -= t;
				}
				ans = ans/26;
			}
			for(j = cnt-1; j >= 0; j--)
				printf("%c",tmp[j]);
		}
		else if(ans <= 18278)
		{
			for(cnt = 0; cnt < 3; cnt++)
			{
				int t = ans%26;
				if(t == 0)
				{
					tmp[cnt] = 'Z';
					ans -= 26;
				}
				else
				{
					tmp[cnt] = 'A'+t-1;
					ans -= t;
				}
				ans = ans/26;
			}
			for(j = cnt-1; j >= 0; j--)
				printf("%c",tmp[j]);
		}
		else if(ans <= 475254)
		{
			for(cnt = 0; cnt < 4; cnt++)
			{
				int t = ans%26;
				if(t == 0)
				{
					tmp[cnt] = 'Z';
					ans -= 26;
				}
				else
				{
					tmp[cnt] = 'A'+t-1;
					ans -= t;
				}
				ans = ans/26;
			}
			for(j = cnt-1; j >= 0; j--)
				printf("%c",tmp[j]);
		}
		else if(ans <= 12356630)
		{
			for(cnt = 0; cnt < 5; cnt++)
			{
				int t = ans%26;
				if(t == 0)
				{
					tmp[cnt] = 'Z';
					ans -= 26;
				}
				else
				{
					tmp[cnt] = 'A'+t-1;
					ans -= t;
				}
				ans = ans/26;
			}
			for(j = cnt-1; j >= 0; j--)
				printf("%c",tmp[j]);
		}
		else if(ans <= 321272406)
		{
			for(cnt = 0; cnt < 6; cnt++)
			{
				int t = ans%26;
				if(t == 0)
				{
					tmp[cnt] = 'Z';
					ans -= 26;
				}
				else
				{
					tmp[cnt] = 'A'+t-1;
					ans -= t;
				}
				ans = ans/26;
			}
			for(j = cnt-1; j >= 0; j--)
				printf("%c",tmp[j]);
		}
		printf("%s\n",s1);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值