Numerically Speaking
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 766 Accepted Submission(s): 190
Problem Description
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
题意:给你一串数,可能全部是数字或全部是小写字母,它们之间的转化规律是a-z分别表示1-26,然后输出一行,前边22列是小写字母,后边是它所表示的10进制整数,每3位用一个‘,’隔开;
解题思路:简单的10—26进制转换,注意转换是1用a表示0用z表示,数据类型应该是大数,所以我是用java写的比较方便
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
String str;
for(int i =0;;i++){
str = cin.next();///读入一个字符串
if(str.equals("*"))///控制多实例测试,以*结束
break;
String ans = new String();
if(str.charAt(0)>='0'&&str.charAt(0)<='9'){//数字转字母
BigInteger num = new BigInteger(str);///将输入字符串转化成大数类型
for(int j = 0; ;j++){
if(num.equals(BigInteger.ZERO))///先对26取余,再取整,直到num为0结束
break;
BigInteger temp;
temp = num.mod(BigInteger.valueOf(26));///取余
int k = temp.intValue();//转化成整型
num = num.divide(BigInteger.valueOf(26));//取整
char c = (char) (k + 'a'-1);//转化成字母并生成新的字符串
ans += c;
}
char[] cs=ans.toCharArray();///因为转化成26进制是反 的,所以需要反转得到的字符串
String Ans1 = "";
for(int ii = cs.length-1; ii >= 0; ii--)
Ans1=Ans1+cs[ii];
System.out.print(Ans1);
if(Ans1.length()<22){//不足22位用空格补齐
for(int ii =0;ii<22-Ans1.length();ii++){
System.out.print(" ");
}
}
int len = str.length();//将原来的数字按格式要求输出即从个位开始每3位一个','
int tt = len%3;
if(tt==0){
for(int ii=0;iiif(ii%3==0&&ii!=0){
System.out.print(",");
//ii--;
}
System.out.print(str.charAt(ii));
}
}
if(tt==1){
System.out.print(str.charAt(0));
for(int ii=1;iiif((ii-1)%3==0)
System.out.print(",");
System.out.print(str.charAt(ii));
}
}
if(tt==2){
System.out.print(str.charAt(0));
System.out.print(str.charAt(1));
for(int ii=2;iiif((ii-2)%3==0)
System.out.print(",");
System.out.print(str.charAt(ii));
}
}System.out.println();
}
else{//字母转数字
System.out.print(str);//输出字符串,不足22位用空格补齐
int lem = str.length();
if(str.length()<22){
for(int ii =0;ii<22-str.length();ii++){
System.out.print(" ");
}
}
BigInteger sum,c;
c = BigInteger.valueOf(26);
sum = BigInteger.ZERO;
char[] cs=str.toCharArray();//将原字符串转化成字符数组,方便后边的反转
String str1 = "";
for(int ii = cs.length-1; ii >= 0; ii--){//将原字符串反转
str1=str1+cs[ii];
}
for(int ii = 0;ii//将26进制转化成10进制用大数类型存着
int t = (int)(str1.charAt(ii)-'a'+1);
BigInteger tt = c.pow(ii);
tt = tt.multiply(BigInteger.valueOf(t));
sum = sum.add(tt);
}
String ss;
ss = sum.toString();//将大数转换成字符串
int len = ss.length();
int tt = len%3;
if(tt==0){//按格式要求输出数字
for(int ii=0;iiif(ii%3==0&&ii!=0){
System.out.print(",");
}
System.out.print(ss.charAt(ii));
}
}
if(tt==1){
System.out.print(ss.charAt(0));
for(int ii=1;iiif((ii-1)%3==0)
System.out.print(",");
System.out.print(ss.charAt(ii));
}
}
if(tt==2){
System.out.print(ss.charAt(0));
System.out.print(ss.charAt(1));
for(int ii=2;iiif((ii-2)%3==0)
System.out.print(",");
System.out.print(ss.charAt(ii));
}
}
System.out.println();
}
}
}
}