ACM之Java输入输出(二)

2014-11-24 07:48:26 · 作者: · 浏览: 2

Sample Input

4

+ 1 2

- 1 2

* 1 2

/ 1 2

Sample Output

3

-1

2

0.50

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc =new Scanner(System.in);

int n = sc.nextInt();

for(int i=0;i

String op = sc.next();

int a = sc.nextInt();

int b = sc.nextInt();

if(op.charAt(0)=='+'){

System.out.println(a+b);

}else if(op.charAt(0)=='-'){

System.out.println(a-b);

}else if(op.charAt(0)=='*'){

System.out.println(a*b);

}else if(op.charAt(0)=='/'){

if(a % b == 0) System.out.println(a / b);

else System.out.format("%.2f", (a / (1.0*b))). Println();

}

}

}

}

3. 规格化的输出:
函数:
// 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.
DecimalFormat fd = new DecimalFormat("#.00#");
DecimalFormat gd = new DecimalFormat("0.000");
System.out.println("x =" + fd.format(x));
System.out.println("x =" + gd.format(x));

public static void main(String[] args) {

NumberFormat formatter = new DecimalFormat( "000000");

String s = formatter.format(-1234.567); // -001235

System.out.println(s);

formatter = new DecimalFormat( "##");

s = formatter.format(-1234.567); // -1235

System.out.println(s);

s = formatter.format(0); // 0

System.out.println(s);

formatter = new DecimalFormat( "##00");

s = formatter.format(0); // 00

System.out.println(s);

formatter = new DecimalFormat( ".00");

s = formatter.format(-.567); // -.57

System.out.println(s);

formatter = new DecimalFormat( "0.00");

s = formatter.format(-.567); // -0.57

System.out.println(s);

formatter = new DecimalFormat( "#.#");

s = formatter.format(-1234.567); // -1234.6

System.out.println(s);

formatter = new DecimalFormat( "#.######");

s = formatter.format(-1234.567); // -1234.567

System.out.println(s);

formatter = new DecimalFormat( ".######");

s = formatter.format(-1234.567); // -1234.567

System.out.println(s);

formatter = new DecimalFormat( "#.000000");

s = formatter.format(-1234.567); // -1234.567000

System.out.println(s);

formatter = new DecimalFormat( "#,###,###");

s = formatter.format(-1234.567); // -1,235

System.out.println(s);

s = formatter.format(-1234567.890); // -1,234,568

System.out.println(s);

// The ; symbol is used to specify an alternate pattern for negative values

formatter = new DecimalFormat( "#;(#) ");

s = formatter.format(-1234.567); // (1235)

System.out.println(s);

// The ' symbol is used to quote literal symbols

formatter = new DecimalFormat( " '# '# ");

s = formatter.format(-1234.567); // -#1235

System.out.println(s);

formatter = new DecimalFormat( " 'abc '# ");

s = formatter.format(-1234.567); // - abc 1235

System.out.println(s);

formatter = new DecimalFormat( "#.##%");

s = formatter.format(-12.5678987);

System.out.println(s);

}

4. 字符串处理 String

String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始:

String a = "Hello"; // a.charAt(1) = 'e'

用substring方法可得到子串,如上例

System.out.println(a.substring(0, 4)) // output "Hell"

注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。

字符串连接可以直接用 + 号,如

String a = "Hello";

String b = "world";

System.out.println(a + ", " + b + "!"); // output "Hello, world!"

如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。

5. 高精度
BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

import java.io.BufferedInputStream;

import java.math.BigInteger;

import java.util.Scan