http://acm.hdu.edu.cn/showproblem.php?pid=1092
http://acm.hdu.edu.cn/showproblem.php?pid=1093
http://acm.hdu.edu.cn/showproblem.php?pid=1094
2. 输出
函数:
System.out.print();
System.out.println();
System.out.format();
System.out.printf();
例4 杭电1170Balloon Comes!
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively)
and two positive integers, your task is to output the result.
Input
Input contains multiple test cases. The first line of the input is a single integer T (0
}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) {
formatter
formatter.format(-1234.567);
=
new
DecimalFormat( "000000");
//
-001235
=
=
new
NumberFormat
String s =
System.out.println(s);
formatter
s
System.out.println(s);
s
System.out.println(s);
formatter
s
System.out.println(s);
new
=
=
=
new
=
=
=
=
new
formatter
s
System.out.println(s);
formatter
s
System.out.println(s);
formatter
s
System.out.println(s);
formatter
s
new
new
=
=
=
=
DecimalFormat( "##");
formatter.format(-1234.567);
formatter.format(0);
DecimalFormat( "##00");
formatter.format(0);
//
-1235
//
0
//
00
//
-.57
DecimalFormat( ".00");
formatter.format(-.567);
formatter.format(-.567);
DecimalFormat( "0.00");
//
-0.57
DecimalFormat( "#.#");
formatter.format(-1234.567);
//
-1234.6
DecimalFormat( "#.######");
formatter.format(-1234.567);
//
-1234.567
=
=
new
System.out.println(s);
formatter
s
System.out.println(s);
formatter
s
System.out.println(s);
new
=
=
new
=
=
formatter
s
System.out.println(s);
s
System.out.println(s);
=
DecimalFormat( ".######");
formatter.format(-1234.567);
//
-1234.567
DecimalFormat( "#.000000");
formatter.format(-1234.567);
//
-1234.567000
DecimalFormat( "#,###,###");
formatter.format(-1234.567);
//
-1,235
formatter.format(-1234567.890); //
-1,234,568
//
The
;
symbol
used
to
specify
an
alternate
pattern
for
negative
is
values
new
=
formatter
s
System.out.println(s);
=
DecimalFormat( "#;(#) ");
formatter.format(-1234.567);
//
(1235)
is
used
to
quote
literal
symbols
DecimalFormat( " '# '# ");
formatter.format(-1234.567);
//
-#1235
DecimalFormat( " 'abc '# ");
formatter.format(-1234.567);
// - abc 1235
=
=
'
The
symbol
new
//
formatter
s
System.out.println(s);
formatter
s
System.out.println(s);
new
=
=
new
=
formatter
s
System.out.println(s);
=
formatter.format(-12.5678987);
DecimalFormat( "#.##%");
}
http://acm.hdu.edu.cn/showproblem.php?pid=1012
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.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int a = 123, b = 456, c = 7890;
BigInteger x, y, z, ans;
x = BigInteger.valueOf(a);
y = BigInteger.valueOf(b);
z = BigInteger.valueOf(c);
ans = x.add(y); System.out.println(ans);
ans = z.divide(y); System.out.println(ans);
ans = x.mod(z); System.out.println(ans);
if (ans.compareTo(x) == 0) System.out.println("1");
}
}
http://acm.hdu.edu.cn/showproblem.php?pid=1018
http://acm.hdu.edu.cn/showproblem.php?pid=1013
http://acm.hdu.edu.cn/showproblem.php?pid=1002
6. 进制转换
java很强大的一个功能。
函数:
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,
第一个为要转的字符串,第二个为说明是什么进制).
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.
7. 数组排序
函数:Arrays.sort();
public class Main {
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int n = cin.nextInt();
int a[] = new int [n];
for (int i = 0; i < n; i++) a[i] = cin.nextInt();
Arrays.sort(a);
for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
public static void main(String[] args)
{
}
}