Blue Bridge Cup past exam questions Excel address

Problem Description

  The address representation of Excel cells is interesting, it uses letters to represent column numbers.
  For example,
  A means column 1,
  B means column 2,
  Z means column 26,
  AA means column 27,
  AB means column 28,
  BA means column 53,
  ....


  Of course, the maximum column number in Excel is limited, so it is not difficult to convert.
  What if we wanted to generalize this notation to convert very large numbers into very long sequences of letters?


  This topic is to ask for the input number, output its corresponding Excel address representation.

sample input

26

Sample output

WITH

sample input

2054

Sample output

BZZ

Data size and conventions

  We agree that the input integer range [1,2147483647]


  peak memory consumption (including virtual machine) < 256M
  CPU consumption < 1000ms

       Time limit: 1.0s  

       Memory limit: 256.0MB

  Please output strictly according to the requirements, and do not superfluously print superfluous content like: "Please enter...".

// 进制转换,将十进制转为26进制,对应数位上的数字加上'A'
import java.util.ArrayList;
import java.util.Scanner;

public class Mian {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Character> list = new ArrayList<Character>();
		while (n != 0) {
			int temp = n % 26;
			if (temp == 0)
				temp = 26;
			list.add((char) ('A' + temp - 1));
			n = (n - temp) / 26;
		}
		for (int i = list.size() - 1; i >= 0; i--)
			System.out.print(list.get(i));

		sc.close();
	}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326036018&siteId=291194637