首页 > 其他 > 详细

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

时间:2014-04-12 13:10:41      阅读:640      评论:0      收藏:0      [点我收藏+]

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;
}



poj 2273 An Excel-lent Problem(进制转换),布布扣,bubuko.com

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

原文:http://blog.csdn.net/u013081425/article/details/23470635

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!