CODEFORCES:1B. Spreadsheets

接着上次的说,我是在做这一题的时候才发现CODEFORCES上有submissions这一个选项,可以查看自己在哪一步出了错误;

接下来直接入主题,来看看这道题:

“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.”
简单地说,就是Excel的row,cloumn的表示方式与RXCY的表示方式的切换:
我的想法是:按输入的字符的不同类型去调用RtoE()与EtoR()方法,那么怎么能把他们区分开呢?例如:“R23C55“,”BC55“,怎么把它们分开呢?
我最初的想法是首先判断第一个是字符,第二个不是字符,那么就把它归为R的表示方法,然后调用RtoE()即可(注:上面出现的错误也说明这种方法有缺陷)
那么我的RtoE方法应该怎么写,想想,在这里我参考了 xujin的方法:
704%26=2-->B;

704/26=27;

27%26=1->A;

27/26=1;

1%26=1->A;

1/26=0;

此时c=0,结束循环

然后利用栈,倒着来输出。


注意到如果涉及到Z,就有点不同了。

如果余0,代表Z,然后令c--才是正确结果,

比如702代表ZZ,转换过程如下:

702%26=0-->Z;

704/26=27;

27--;

26%26=0->Z;

26/26=1;

1--;

此时c=0,结束循环
事实证明这个方法是行得通的:
private String RtoE(String s)          //RtoE方法将RXCY表示变化为Excel方法表示
	{
		byte ValueByte[] = s.getBytes();
		int i = 1;
		long pre = 0, next = 0;
		while(i < s.length() && ValueByte[i] != 'C')
		{
				i++;                          //使用i来保存'C'在字符串中的位置
		}
		String SubStrPre = s.substring(1, i);  
		String SubStrNext = s.substring(i+1);
		pre = Long.valueOf(SubStrPre);          //得到row
		next = Long.valueOf(SubStrNext);         //得到cloumn
		StringBuffer sb = new StringBuffer();
		int temple = 0;
		do
		{
			temple = (int)(next % 26);
			if(0 == temple)
			{
				sb.append('Z');
				next = next / 26;
				next--;
			}
			else
			{
				char ch = (char)('A' + temple - 1);
				sb.append(ch);
				next = next / 26;
			}
		}while(next != 0);
		StringBuffer bs = new StringBuffer(sb.reverse());      //使用bs得到sb的倒置
		return bs + String.valueOf(pre);                      //返回
	}

另外有一个EtoR这个比较简单,这里就不多说了:
private String EtoR(String s)
	{
		byte ValueByte[] = s.getBytes();
		int i = 1;
		while((ValueByte[i] >= 'A') && (ValueByte[i] <= 'Z') )
		{
			i++;                                 //用i记录输入字符串中的前面的字符的个数
		}
//		String SubStrPre = s.substring(0, i);
		String SubStrNext = s.substring(i);
//		System.out.println(s);
		long next = Long.valueOf(SubStrNext);            //得到row
		long pre = (long)(ValueByte[0] - 'A'+1);
		for(int j = 1; j < i; j++)
		{
//			System.out.println(ValueByte[j] - 'A'+1);
			pre = pre * 26 + (ValueByte[j] - 'A'+1);
//			System.out.println(pre);
		}                                                 //得到cloumn
		return 'R'+String.valueOf(next)+'C'+String.valueOf(pre);
	}

最后还有一点比较重要的要说,也是必须要说的:我在开头就给出了我的错误(我上面也说过了,我的判断方法有问题),即当他给出”A1“的时候,用上面的判断方法的话会调用RtoE,而事实上,它应该调用EtoR的方法,所以只好在判断时给出了特定情况,也就是第一个为字符,第二个不为字符,但后面也没有字符的话把它归类为E,应该调用EtoR:
public static void main(String[] args)
	{
		Spreadsheets ss = new Spreadsheets();
		Scanner in = new Scanner(System.in);
//		int num = in.nextInt();
		String test = in.nextLine();
		int num = Integer.valueOf(test);
		String str = new String();
		for(int i = 0; i < num; i++)
		{
			String s = in.nextLine();
			byte VB[] = s.getBytes();
			if(VB[1] <= '9' && VB[1] >'0')
			{
				boolean flag = false;
				for(int j = 1; j < VB.length; j++)
				{
					if(VB[j] > 'A' && VB[j] < 'Z')
						flag = true;
				}
				if(flag)
				{
					str = ss.RtoE(s);               //判断s为RXCY
				}
				else
				{
					str = ss.EtoR(s);               //判断s为Excel
				}
			}
			else
			{
				str = ss.EtoR(s);                   //判断s为Excel
			}
			System.out.println(str);
			str = new String();
		}
	}

那么这样就ok了



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值