# Java for ACM 小结

### 一、提交格式代码

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in));
}
}

### 二、输入

cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble()      // 判断是否有下一个输入可以用
int n = cin.nextInt();
String s = cin.next();
double t = cin.nextDouble();
String s = cin.nextLine();

### 三、浮点控制

DecimalFormat g = new DecimalFormat("0.00");
double a = 123.45678;
System.out.println(g.format(a));        // 输出 123.46


### 四、高精度

import java.math.*;     // 需要引入math库
BigInteger a = BigInteger.valueOf(1000000);
BigInteger b = BigInteger.valueOf(10000);
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
System.out.println(a.divide(b));
System.out.println(a.mod(b));
System.out.println(a.compareTo(b));		// 返回值 -1,0,1


### 五、字符串

s.substring(a,b);

String a = "0123456789";
System.out.println(a.substring(2,5));        // 输出 234

### 六、数组

int[] a = new int[123];
Arrays.fill(a,123);
System.out.println(a[3]);     // 输出 123

## 一个例题

import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
while ( cin.hasNextBigDecimal() ){
BigDecimal a = cin.nextBigDecimal();
String op = cin.next();
BigDecimal b = cin.nextBigDecimal();
if ( op.equals("+") ){
System.out.println(a.add(b));
}else
if ( op.equals("-") ){
System.out.println(a.subtract(b));
}else
if ( op.equals("*") ){
System.out.println(a.multiply(b));
}else
if ( op.equals("/") ){
int c = cin.nextInt();
BigDecimal res[] = a.divideAndRemainder(b);
System.out.println(res[0]+" "+res[1]+" "+a.divide(b,c,BigDecimal.ROUND_HALF_UP).toPlainString());
}
}
}
}

##### huiren
###### Code Artisan

comments powered by Disqus