CODEFORCES:1B. Spreadsheets

本文详细解析了一种将Excel的row、column表示方式与RXCY表示方式之间进行切换的算法实现,包括R到E(Excel表示)与E到R(RXCY表示)的转换方法,以及如何通过字符区分输入数据属于哪种表示方式,同时解决特殊情况下的错误判断问题。

接着上次的说,我是在做这一题的时候才发现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了



基于模拟退火的计算器 在线运行 访问run.bcjh.xyz。 先展示下效果 https://pan.quark.cn/s/cc95c98c3760 参见此仓库。 使用方法(本地安装包) 前往Releases · hjenryin/BCJH-Metropolis下载最新 ,解压后输入游戏内校验码即可使用。 配置厨具 已在2.0.0弃用。 直接使用白菜菊花代码,保留高级厨具,新手池厨具可变。 更改迭代次数 如有需要,可以更改 中39行的数字来设置迭代次数。 本地编译 如果在windows平台,需要使用MSBuild编译,并将 改为ANSI编码。 如有条件,强烈建议这种本地运行(运行可加速、可多次重复)。 在 下运行 ,是游戏中的白菜菊花校验码。 编译、运行: - 在根目录新建 文件夹并 至build - - 使用 (linux) 或 (windows) 运行。 最后在命令行就可以得到输出结果了! (注意顺序)(得到厨师-技法,表示对应新手池厨具) 注:linux下不支持多任务选择 云端编译已在2.0.0弃用。 局限性 已知的问题: - 无法得到最优解! 只能得到一个比较好的解,有助于开阔思路。 - 无法选择菜品数量(默认拉满)。 可能有一定门槛。 (这可能有助于防止这类辅助工具的滥用导致分数膨胀? )(你问我为什么不用其他语言写? python一个晚上就写好了,结果因为有涉及json读写很多类型没法推断,jit用不了,算这个太慢了,所以就用c++写了) 工作原理 采用两层模拟退火来最大化总能量。 第一层为三个厨师,其能量用第二层模拟退火来估计。 也就是说,这套方法理论上也能算厨神(只要能够在非常快的时间内,算出一个厨神面板的得分),但是加上厨神的食材限制工作量有点大……以后再说吧。 (...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值