首页 > 专题系列 > Java解POJ > POJ 2273 An Excel-lent Problem [解题报告] Java
2013
11-11

POJ 2273 An Excel-lent Problem [解题报告] Java

An Excel-lent Problem

问题描述 :

A certain spreadsheet program labels the columns of a spreadsheet using letters. Column 1 is labeled as “A”, column 2 as “B”, …, column 26 as “Z”. When the number of columns is greater than 26, another letter is used. For example, column 27 is “AA”, column 28 is “AB” and column 52 is “AZ”. It follows that column 53 would be “BA” and so on. Similarly, when column “ZZ” is reached, the next column would be “AAA”, then “AAB” and so on.

The rows in the spreadsheet are labeled using the row number. Rows start at 1.

The designation for a particular cell within the spreadsheet is created by combining the column label with the row label. For example, the upper-left most cell would be “A1″. The cell at column 55 row 23 would be “BC23″.

You will write a program that converts numeric row and column values into the spreadsheet designation.

输入:

Input consists of lines of the form: RnCm. n represents the row number [1,300000000] and m represents the column number, 1<=m<=300000000. The values n and m define a single cell on the spreadsheet. Input terminates with the line: R0C0 (that is, n and m are 0). There will be no leading zeroes or extra spaces in the input.

输出:

For each line of input (except the terminating line), you will print out the spreadsheet designation for the specified cell as described above.

样例输入:

R1C1
R3C1
R1C3
R299999999C26
R52C52
R53C17576
R53C17602
R0C0

样例输出:

A1
A3
C1
Z299999999
AZ52
YYZ53
YZZ53

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
	Scanner in = new Scanner(System.in);
	while(true)
	{
		String input = in.nextLine();
		if(input.equals("R0C0"))
			break;
		int index = 0;
		while(input.charAt(index)!='C')
		{
			index++;
		}
		int num = Integer.parseInt(input.substring(1, index));
		int letter = Integer.parseInt(input.substring(index+1, input.length()));
			
		String result = "";
		result = change(letter) + result;
		while(true)
		{
			if(letter/26.0>1)
			{
				if(letter%26!=0)
				{
					letter = letter/26;
					result = change(letter) + result;
				}
				else
				{
					letter = letter/26;
					letter--;
					result = change(letter) + result;
				}
			}
			else
				break;
		}
		System.out.println(result+num);
	}
   }
	
  public static char change(int letter)
  {
	char m;
	if(letter % 26 == 0)
		m = 'Z';
	else
		m = (char) (letter%26 + 'A' -1);
	return m;
  }
}

  1. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  2. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  3. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3