一、Java之ACM注意点
1. 类名称必须采用public class Main方式命名
2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾
3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件
4. 在有多行数据输入的情况下,一般这样处理,
- static Scanner in = new Scanner(System.in);
- while(in.hasNextInt())
- 或者是
- while(in.hasNext())
5. 有关System.nanoTime() 函数的使用,该函数用来 返回最准确的可用系统计时器的当前值,以毫微秒为单位。
- long startTime = System.nanoTime();
- // ... the code being measured ...
- long estimatedTime = System.nanoTime() - startTime;
二、Java之输入输出处理
由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。
1. 输入:
格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in));
格式2:Scanner sc = new Scanner (System.in);
在读入数据量大的情况下,格式1的速度会快些。
读一个整数: int n = sc.nextInt(); 相当于 scanf("%d", &n); 或 cin >> n;
读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s;
读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t;
读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(...);
判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()
例1:读入整数
|
例2:读入实数
输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。
|
例3:读入字符串【杭电2017 字符串统计】
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
|
例3:读入字符串【杭电2005 第几天?】
|
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<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.
Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.
Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2
Sample Output
3
-1
2
0.50
|
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));
|
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().
|
6. 进制转换
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();
|
8. 其他注意的事项
(1) Java 是面向对象的语言,思考方法需要变换一下,里面的函数统称为方法,不要搞错。
(2) Java 里的数组有些变动,多维数组的内部其实都是指针,所以Java不支持fill多维数组。
数组定义后必须初始化,如 int[] a = new int[100];
(3) 布尔类型为 boolean,只有true和false二值,在 if (...) / while (...) 等语句的条件中必须为boolean类型。
在C/C++中的 if (n % 2) ... 在Java中无法编译通过。
(4) 下面在java.util包里Arrays类的几个方法可替代C/C++里的memset、qsort/sort 和 bsearch:
Arrays.fill()
Arrays.sort()
Arrays.binarySearch()
原帖地址:http://blog.csdn.net/shijiebei2009/article/details/17305223
===================================================================================================================
Java:
1.先输入数组长度n,在输入n个数字
import java.util.Scanner;2.输入不定长数组:
Scanner sc=new Scanner(System.in);
int n;
n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
import java.util.ArrayList;3.数字转字符串
import java.util.Scanner;
Scanner sc=new Scanner(System.in);
ArrayList<Integer> a=new ArrayList<Integer>();
while(sc.hasNextLine()){
int e=sc.nextInt();
if(e==0) break;
a.add(e);
}
String s = String.valueOf( value);===================================================================================================================
字符串转数字
int num = Integer.parseInt(str);
1、基本定义
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin1 = new Scanner(System.in);
Scanner cin2 = new Scanner(new BufferedInputStream(System.in));
}
}
使用cin2进行输入的时候可能会比cin1快一些。
2、输入具体数据
(1)输入一个整数:int n = cin.nextInt();
(2)输入一个字符串:String s = cin.next();
(3)输入一个浮点数:double f = cin.nextDouble();
(4)读入一整行:String s = cin.nextLine();
判断是否有下一个输入,可以用cin.hasNext()或cin.hasNextInt()或cin.hasNextDouble()等进行判断。
3、基本输出
(1)System.out.print(); //类似于cout<<…….;
(2)System.out.println(); //类似于cout<<……<<endl;
(3)System.out.printf(); //类似于C中printf的功能
样例:
- import java.io.*;
- import java.math.*;
- import java.util.*;
- import java.text.*;
- public class Main {
- public static void main(String[] args) {
- Scanner cin = new Scanner(new BufferedInputStream(System.in));
- int a;
- double b;
- a = 12345;
- b = 1.234567;
- System.out.println(a + " " + b);
- System.out.printf("%d %10.5f\n", a, b);
- // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入.
- }
- }
输出结果:
12345 1.234567
12345 1.23457
4、要求具体精度的输出
(1)可以使用上面介绍的System.out.printf();
(2)对于输出浮点数要保留几位小数的问题,可以使用DecimalFormat类解决
- import java.util.*;
- import java.text.*;
- public class Main {
- public static void main(String[] args) {
- DecimalFormat f = new DecimalFormat("#.00#");
- DecimalFormat g = new DecimalFormat("0.000");
- // 这里的0指一位数字,#指除0以外的数字
- double a = 123.456789, b = 0.123456;
- System.out.println(f.format(a));
- System.out.println(f.format(b));
- System.out.println(g.format(a));
- System.out.println(g.format(b));
- }
- }
输出结果:
123.457
.123
123.457
0.123
5、字符串的处理
(1)String
Java中字符串String是不可以修改的,要修改只能转换为字符数组。
String st = "abcdefg";
char[] ch;
ch = st.toCharArray(); // 字符串转换为字符数组.
6、高精度问题
7、大数问题
Java中有两个类BigDecimal(表示浮点数)和BigInteger(表示整数)
使用这两个类的时候需要加上import java.math.*;
Ⅰ基本函数:
(1)valueOf(parament); 将参数转换为指定类型
例如:
int a = 3;
BigInteger b = BigInteger.valueOf(a);
即b = 3
String s = “1234”;
BigInteger b = BigInteger.valueOf(s);
即b = 1234
(2)add(); //大数加法
例如:
BigInteger a = new BigInteger(“11”);
BigInteger b = new BigInteger(“22”);
a.add(b);
即a = 33
(3)substract(); //减法
(4)multiply(); //乘法
(5)divided(); //相除取整
(6)remainder(); //取余
(7)pow(); //a.pow(b) = a ^ b
(8)gcd(); //最大公约数
(9)abs(); //绝对值
(10)negate(); //取反数
(11)mod(); //a.mod(b) = a % b = a.remainder(b)
(12)max(); min();
(13)public int compareTo(); //比较
(14)boolean equals(); //比较是否相等
(15)BigIntergerde 构造函数
一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;
BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
例如:
BigInteger b = new BigInteger("1010",2);
System.out.println(b);
输出:10
Ⅱ.基本常量:
A=BigInteger.ONE //=1
B=BigInteger.TEN //=10
C=BigInteger.ZERO //=0
Ⅲ.基本操作
1. 读入:
- while(cin.hasNext()) //等同于!=EOF
- {
- int n;
- BigInteger m;
- n=cin.nextInt(); //读入一个int;
- m=cin.BigInteger();//读入一个BigInteger;
- System.out.print(m.toString());
- System.out.print(m);
- }
Ⅳ.运用
四则预算:
- import java.util.Scanner;
- import java.math.*;
- import java.text.*;
- public class Main {
- public static void main(String args[]) {
- Scanner cin = new Scanner(System.in);
- BigInteger a, b;
- int c;
- char op;
- String s;
- while (cin.hasNext()) {
- a = cin.nextBigInteger();
- s = cin.next();
- op = s.charAt(0);
- if (op == '+') {
- b = cin.nextBigInteger();
- System.out.println(a.add(b));
- } else if (op == '-') {
- b = cin.nextBigInteger();
- System.out.println(a.subtract(b));
- } else if (op == '*') {
- b = cin.nextBigInteger();
- System.out.println(a.multiply(b));
- } else {
- BigDecimal a1, b1, eps;
- // 浮点数
- String s1, s2, temp;
- s1 = a.toString();
- a1 = new BigDecimal(s1);
- b = cin.nextBigInteger();
- s2 = b.toString();
- b1 = new BigDecimal(s2);
- c = cin.nextInt();
- // 接收精度控制,即保留几位小数的问题
- eps = a1.divide(b1, c, 4);
- if (c != 0) {
- temp = "0.";
- for (int i = 0; i < c; i++)
- temp += "0";
- DecimalFormat gd = new DecimalFormat(temp);
- System.out.println(gd.format(eps));
- } else
- System.out.println(eps);
- }
- }
- }
- }