【HDOJ】1314 Numerically Speaking

学了几天的Java了,终于独立A了一道大数计算。感觉还得练Java啊。

 1 import java.util.Scanner;
 2 import java.math.BigInteger;
 3 import java.lang.StringBuilder;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner cin = new Scanner(System.in);
 8         while (cin.hasNextLine()) {
 9             String line = cin.nextLine();
10             char l0 = line.charAt(0);
11             if (l0 == '*')
12                 break;
13             if (l0>='0' && l0<='9') {
14                 StringBuilder builder = new StringBuilder();
15                 BigInteger x = new BigInteger(line);
16                 BigInteger div = new BigInteger("26");
17                 BigInteger[] vals = new BigInteger[2];
18                 while (x.compareTo(BigInteger.ZERO) != 0) {
19                     vals = x.divideAndRemainder(div);
20                     char ch = (char)(vals[1].intValue()+96);
21                     builder.append(ch);
22                     x = vals[0];
23                 }
24                 builder.reverse();
25                 String ans = new String(builder.toString());
26                 System.out.print(ans);
27                 int l = ans.length();
28                 while (l < WIDTH) {
29                     System.out.print(' ');
30                     ++l;
31                 }
32             } else {
33                 int length = line.length();
34                 System.out.print(line);
35                 int l = length;
36                 while (l < WIDTH) {
37                     System.out.print(' ');
38                     ++l;
39                 }
40                 BigInteger div = new BigInteger("26");
41                 BigInteger sum = new BigInteger(String.valueOf((int)(line.charAt(length-1))-96));
42                 for (int i=length-2; i>=0; --i) {
43                     // System.out.print("current sum = ");
44                     // System.out.println(sum.toString());
45                     BigInteger tmp = div.multiply(new BigInteger(String.valueOf((int)(line.charAt(i))-96)));
46                     sum = sum.add(tmp);
47                     div = div.multiply(new BigInteger("26"));
48                 }
49                 line = new String(sum.toString());
50             }
51             int length = line.length();
52             int x = length%3;
53             x = (x==0) ? 3:x;
54             int j = 0;
55             for (int i=0; i<length; ++i) {
56                 if (i < x) {
57                     System.out.print(line.charAt(i));
58                 } else {
59                     if ((j+1)<length && (j%3)==0) {
60                         System.out.print(',');
61                     }
62                     ++j;
63                     System.out.print(line.charAt(i));
64                 }
65             }
66             System.out.println();
67         }
68     }
69     public static void reverse(byte[] bytes, int l) {
70         byte tmp;
71         for (int i=0; i+i<l; ++i) {
72             tmp = bytes[i];
73             bytes[i] = bytes[l-1-i];
74             bytes[l-1-i] = tmp;
75         }
76     }
77     static final int WIDTH = 22;
78 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4068309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值