【HDOJ】1314 Numerically Speaking

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

 import java.util.Scanner;
import java.math.BigInteger;
import java.lang.StringBuilder; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNextLine()) {
String line = cin.nextLine();
char l0 = line.charAt();
if (l0 == '*')
break;
if (l0>='' && l0<='') {
StringBuilder builder = new StringBuilder();
BigInteger x = new BigInteger(line);
BigInteger div = new BigInteger("");
BigInteger[] vals = new BigInteger[];
while (x.compareTo(BigInteger.ZERO) != ) {
vals = x.divideAndRemainder(div);
char ch = (char)(vals[].intValue()+);
builder.append(ch);
x = vals[];
}
builder.reverse();
String ans = new String(builder.toString());
System.out.print(ans);
int l = ans.length();
while (l < WIDTH) {
System.out.print(' ');
++l;
}
} else {
int length = line.length();
System.out.print(line);
int l = length;
while (l < WIDTH) {
System.out.print(' ');
++l;
}
BigInteger div = new BigInteger("");
BigInteger sum = new BigInteger(String.valueOf((int)(line.charAt(length-))-));
for (int i=length-; i>=; --i) {
// System.out.print("current sum = ");
// System.out.println(sum.toString());
BigInteger tmp = div.multiply(new BigInteger(String.valueOf((int)(line.charAt(i))-)));
sum = sum.add(tmp);
div = div.multiply(new BigInteger(""));
}
line = new String(sum.toString());
}
int length = line.length();
int x = length%;
x = (x==) ? :x;
int j = ;
for (int i=; i<length; ++i) {
if (i < x) {
System.out.print(line.charAt(i));
} else {
if ((j+)<length && (j%)==) {
System.out.print(',');
}
++j;
System.out.print(line.charAt(i));
}
}
System.out.println();
}
}
public static void reverse(byte[] bytes, int l) {
byte tmp;
for (int i=; i+i<l; ++i) {
tmp = bytes[i];
bytes[i] = bytes[l--i];
bytes[l--i] = tmp;
}
}
static final int WIDTH = ;
}
上一篇:【趣事】用 JavaScript 对抗 DDOS 攻击 (下)


下一篇:linux安装python