这里指的Java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目。
- 1. 输入:
-
- 格式为:Scanner cin = new Scanner (new BufferedInputStream(System.in));
-
- 例程:
-
- 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; BigInteger c; String st;
-
- a = cin.nextInt(); b = cin.nextDouble(); c = cin.nextBigInteger(); d = cin.nextLine();
-
- }
-
- }
-
-
-
- 2. 输出
-
- 函数:System.out.print(); System.out.println(); System.out.printf();
-
- System.out.print();
-
- System.out.println();
-
- System.out.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);
-
- }
-
- }
-
- 规格化的输出:
-
- 函数:
-
-
-
- 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));
-
-
-
- 3. 字符串处理
-
- java中字符串String是不可以修改的,要修改只能转换为字符数组.
-
- 例程:
-
- import java.io.*;
-
- import java.math.*;
-
- import java.util.*;
-
- import java.text.*;
-
- public class Main
-
- {
-
- public static void main(String[] args)
-
- {
-
- int i;
-
- Scanner cin = new Scanner (new BufferedInputStream(System.in));
-
- String st = "abcdefg";
-
- System.out.println(st.charAt(0));
-
- char [] ch;
-
- ch = st.toCharArray();
-
- for (i = 0; i < ch.length; i++) ch[i] += 1;
-
- System.out.println(ch);
-
- if (st.startsWith("a"))
-
- {
-
- st = st.substring(1);
-
- }
-
- }
-
- }
-
-
-
- 4. 高精度
-
- BigInteger和BigDecimal可以说是acmer选择java的首要原因。
-
- 函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().
-
- 例程:
-
- 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 = 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");
-
- }
-
- }
-
-
-
- 5. 进制转换
-
- java很强大的一个功能。
-
- 函数:
-
- String st = Integer.toString(num, base);
-
- int num = Integer.parseInt(st, base);
-
- BigInter m = new BigInteger(st, base);
-
-
-
- 1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);
-
- 当然也可以使用其他进制方式读入;
-
- 2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);
-
- 例程:POJ 2305
-
-
-
- import java.io.*;
-
- import java.util.*;
-
- import java.math.*;
-
- public class Main
-
- {
-
- public static void main(String[] args)
-
- {
-
- int b;
-
- BigInteger p,m,ans;
-
- String str ;
-
- Scanner cin = new Scanner (new BufferedInputStream(System.in));
-
- while(cin.hasNext())
-
- {
-
- b=cin.nextInt();
-
- if(b==0)
-
- break;
-
- p=cin.nextBigInteger(b);
-
- m=cin.nextBigInteger(b);
-
- ans=p.mod(m);
-
- str=ans.toString(b);
-
- System.out.println(str);
-
- }
-
- }
-
- }
-
-
-
-
-
- 6. 排序
-
- 函数:Arrays.sort();至于怎么排序结构体,像C++里写个cmp的方法,在java还不太清楚,希望有人指点下~~
-
- 例程:
-
- 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 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] + " ");
-
- }
-
- }
-
-
7. POJ高精度题目汇总:
POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199
转自:http://hi.baidu.com/czyuan_acm/blog/item/d0bf7a439d90d21b72f05d69.html
acm中Java的应用
Chapter I.
Java的优缺点各种书上都有,这里只说说用Java做ACM-ICPC的特点:
(1) 最明显的好处是,学会Java,可以参加Java Challenge :)
(2) 对于熟悉C/C++的程序员来说,Java 并不难学,找本书,一两周业余时间就可以搞定了。当然,这里只是指一般编程,想熟悉所有的Java库还是需要些时间的。
事实上,Java 只相当于C++的一个改进版,所有的语法都几乎是C++的,很少有变动。
(3) 在一般比赛中,Java程序会有额外的时间和空间,而实际上经过实验,在执行计算密集任务的时候Java并不比C/C++慢多少,只是IO操作较慢而已。
(4) Java 简单而功能强大,有些东西用Java实现起来更为方便,比如高精度。
(5) 用Java不易犯细微的错误,比如C/C++中的指针, “if (n = m) ... ” 等
(6) 目前来看Eclipse已成基本配置,写Java程序反而比C/C++更方便调试。在具体竞赛时也算多一种选择。
(7) 学会Java对以后工作有好处。现在国外很多地方会Java的人比会C/C++的人多。
(8) 会Java可以使你看起来更像偶蹄类动物(牛) hoho~
对结构体自定义排序
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
//这个类要写在Main类外面
class node implements Comparable
{
public int x;
public int y;
public node(int xx, int yy)
{
this.x = xx;
this.y = yy;
}
@Override
public int compareTo(Object obj) {
if(obj instanceof node)
{
node b = (node) obj;
//先按x从小到大,相同再按y从小到大
if(this.x == b.x)
{
if(this.y > b.y) return 1;
else return -1;
}
else if(this.y > b.y) return 1;
else return -1;
}
return 0;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
node a[] = new node[500];
int n;
while(sc.hasNext())
{
n = sc.nextInt();
for(int i = 0; i < n; i++)
{
int x, y;
x = sc.nextInt();
y = sc.nextInt();
a[i] = new node(x, y);
}
Arrays.sort(a, 0, n);
for(int i = 0; i < n; i++)
System.out.println(a[i].x + " " + a[i].y);
}
// int n, a[] = new int[500];
// while(sc.hasNext())
// {
// n = sc.nextInt();
// for(int i = 0; i < n; i++)
// a[i] = sc.nextInt();
// Arrays.sort(a, 2, 5); // 第三道第五个元素排序
// for(int i = 0; i < n; i++)
// System.out.println(a[i]);
//memset
Arrays.fill(a, 0);
for(int i = 0; i < n; i++)
System.out.println(a[i]);
// }
}
}
读入挂:
公式是这样的 8*n^2 - 7*n + 1
不过出题人似乎故意卡java来增加题目的难度。。。
作为一个ACM弱渣。。。本屌到今天贡献了2次TLE之后才知道原来 java 里面 bufferReader比Scanner要快。。。原因是因为,前者是读入到缓冲区中,而后者直接读入到硬盘里。。。
Sacnner:是一个基于正则表达式的文本扫描器,可以从文件、输入流、字符串中解析出基本类型值和字符串值。
BufferedReader:是javaIO流中的一个字符串、包装类,它必须建立在另一个字符流的基础之上,但system.in是字节流,需用InputStreamReader将其包装成字符流。
要使用BufferReader输入一些字符之外的类型的数据,就要相对比较麻烦,需要通过一些XXXX.parseXxx();来转换相应的数据类型,虽然,麻烦一些,但通过在一些OJ系统上的和Scanner作对比,BufferReader的效率要比Scanner高一倍,这个差距可想而知,读取的数据越多,效果就越明显。
AC程序
- import java.io.*;
- import java.math.*;
- public class Main{
- public static BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
- public static BufferedWriter cout=new BufferedWriter(new OutputStreamWriter(System.out));
- public static void main(String []args) throws Exception{
- int T=Integer.parseInt(cin.readLine());
- for(int nkase=1;nkase<=T;nkase++){
- cout.write("Case #"+nkase+": ");
- BigInteger N=new BigInteger(cin.readLine());
- BigInteger ans=N.multiply(N).multiply(BigInteger.valueOf(8)).subtract(N.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1));
- cout.write(ans.toString());
- cout.newLine();
- }
- cout.flush();
- cout.close();
- }
- }