24h購物| | PChome| 登入
2012-03-05 14:11:12| 人氣710| 回應0 | 上一篇 | 下一篇

[UVA][JAVA][Biginteger] 619 - Numerically Speaking

推薦 0 收藏 0 轉貼0 訂閱站台

  Numerically Speaking 

A developer of crossword puzzles (and other similar word games) has decided to develop a mapping between every possible word with from one to twenty characters and unique integers. The mapping is very simple, with the ordering being done first by the length of the word, and then alphabetically. Part of the list is shown below.

    a          1
    b          2
    ...
    z          26
    aa         27
    ab         28
    ...
    snowfall   157,118,051,752
    ...

Your job in this problem is to develop a program which can translate, bidirectionally, between the unique word numbers and the corresponding words.

Input 

Input to the program is a list of words and numbers, one per line starting in column one, followed by a line containing a single asterisk in column one. A number will consist only of decimal digits (0 through 9) followed immediately by the end of line (that is, there will be no commas in input numbers). A word will consist of between one and twenty lowercase alphabetic characters (a through z).

Output 

The output is to contain a single line for each word or number in the input data. This line is to contain the word starting in column one, followed by an appropriate number of blanks, and the corresponding word number starting in column 23. Word numbers that have more than three digits must be separated by commas at thousands, millions, and so forth.

Sample Input 

29697684282993
transcendental
28011622636823854456520
computationally
zzzzzzzzzzzzzzzzzzzz
*

Sample Output 

elementary            29,697,684,282,993
transcendental        51,346,529,199,396,181,750
prestidigitation      28,011,622,636,823,854,456,520
computationally       232,049,592,627,851,629,097
zzzzzzzzzzzzzzzzzzzz  20,725,274,851,017,785,518,433,805,270


做法 : 看做是26進制數
JAVA速度果然是超慢 1 s ..., 而且大數沒有比較好寫

import java.util.Scanner;
import java.math.BigInteger;

public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String input;
while(keyboard.hasNext()) {
input = keyboard.next();
if(input.charAt(0) == '*') {
break;
}
if(input.charAt(0) >= 'a' && input.charAt(0) <= 'z')
solve1(input);
else
solve2(input);
}
}
public static void solve1(String input) {
BigInteger count = BigInteger.valueOf(0);
BigInteger tmp = BigInteger.valueOf(1);
int i, length = input.length();
for(i = length-1; i >= 0; i--) {
count = count.add(tmp.multiply(BigInteger.valueOf(input.charAt(i)-'a'+1)));
tmp = tmp.multiply(BigInteger.valueOf(26));
}
String output = count.toString();
length = output.length();
int dot = length;
System.out.printf("%-22s", input);
for(i = 0; i < length; i++) {
if(dot%3 == 0 && i != 0)
System.out.write(',');
System.out.write(output.charAt(i));
dot--;
}
System.out.println();
}
public static void solve2(String input) {
BigInteger count = new BigInteger(input);
String output = "";
int tmp, i, length;
while(count.compareTo(BigInteger.valueOf(0)) != 0) {
tmp = (int)count.mod(BigInteger.valueOf(26)).longValue();
output = (char)(tmp == 0 ? 'z' : (tmp+'a'-1)) + output;
count = count.divide(BigInteger.valueOf(26));
}
length = input.length();
int dot = length;
System.out.printf("%-22s", output);
for(i = 0; i < length; i++) {
if(dot%3 == 0 && i != 0)
System.out.write(',');
System.out.write(input.charAt(i));
dot--;
}
System.out.println();
}
}

台長: Morris
人氣(710) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][JAVA][Sanner] 371 - Ackermann Functions
此分類上一篇:[UVA][JAVA][BigNumber] 10083 - Division

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文