Java在ACM中的應用
由於java裏面有一些東西比c/c++方便(尤其是大數據高精度問題,備受廣大ACMer歡迎),所以就可以靈活運用這三種來實現編程,下面是我自己在各種大牛那裏總結了一些
這裏指的java速成,只限於java語法,包括輸入輸出,運算處理,字符串和高精度的處理,進制之間的轉換等,能解決OJ上的一些高精度題目。
1. 輸入:
格式為:Scanner cin = new Scanner (new BufferedInputStream(System.in));
或者、Scanner cin = new Scanner (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(); // cout << …;
System.out.println(); // cout << … << endl;
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位,四舍五入.
}
}
規格化的輸出:
函數:
// 這裏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));
3. 字符串處理
java中字符串String是不可以修改的,要修改只能轉換為字符數組.
例程:
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 int i; 10 Scanner cin = new Scanner (new BufferedInputStream(System.in)); 11 12 String st = "abcdefg"; 13 System.out.println(st.charAt(0)); // st.charAt(i)就相當於st[i]. 14 char [] ch; 15 ch = st.toCharArray(); // 字符串轉換為字符數組. 16 for (i = 0; i < ch.length; i++) ch[i] += 1; 17 System.out.println(ch); // 輸入為“bcdefgh”. 18 if (st.startsWith("a")) // 如果字符串以‘0‘開頭. 19 { 20 st = st.substring(1); // 則從第1位開始copy(開頭為第0位). 21 } 22 } 23 }
5. 進制轉換
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的進制.
//Added by abilitytao
1.如果要將一個大數以2進制形式讀入 可以使用cin.nextBigInteger(2);
2.如果要將一個大數轉換成其他進制形式的字符串 使用cin.toString(2);//將它轉換成2進制表示的字符串
例程:POJ 2305
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 public class Main 5 { 6 public static void main(String[] args) 7 { 8 int b; 9 BigInteger p,m,ans; 10 String str ; 11 12 Scanner cin = new Scanner (new BufferedInputStream(System.in)); 13 while(cin.hasNext()) 14 { 15 b=cin.nextInt(); 16 if(b==0) break; 17 18 p=cin.nextBigInteger(b); 19 20 m=cin.nextBigInteger(b); 21 22 ans=p.mod(m); 23 str=ans.toString(b); 24 System.out.println(str); 25 } 26 } 27 }
6. 排序
函數:Arrays.sort();
例程:
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin = new Scanner (new BufferedInputStream(System.in)); 10 11 int n = cin.nextInt(); 12 int a[] = new int [n]; 13 for (int i = 0; i < n; i++) a[i] = cin.nextInt(); 14 Arrays.sort(a); 15 for (int i = 0; i < n; i++) System.out.print(a[i] + " "); 16 } 17 }
7. 結構體排序:
例子:一個結構體有兩個元素String x,int y,排序,如果x相等y升序,否者x升序。
一、Comparator
強行對某個對象collection進行整體排序的比較函數,可以將Comparator傳遞給Collections.sort或Arrays.sort。
接口方法:這裏也給出了兩種方法,
1 import java.util.*; 2 3 class structSort { 4 5 String x; 6 int y; 7 } 8 9 class cmp implements Comparator<structSort> { 10 public int compare(structSort o1, structSort o2) { 11 12 if (o1.x.compareTo(o2.x) == 0) {//這個相當於c/c++中strcmp(o1.x , o2,x) 13 14 return o1.y - o2.y; 15 } 16 return o1.x.compareTo(o2.x); 17 } 18 } 19 20 public class Main { 21 public static void main(String[] args) { 22 Comparator<structSort> comparator = new Comparator<structSort>() { 23 public int compare(structSort o1, structSort o2) { 24 if (o1.x.compareTo(o2.x) == 0) { 25 return o1.y - o2.y; 26 } 27 return o1.x.compareTo(o2.x); 28 } 29 }; 30 31 Scanner cin = new Scanner(System.in); 32 int n = cin.nextInt(); 33 structSort a[] = new structSort[10]; 34 for (int i = 0; i < n; i++) { 35 a[i] = new structSort(); 36 a[i].x = cin.next(); 37 a[i].y = cin.nextInt(); 38 } 39 Arrays.sort(a, 0, n, comparator);//這個直接使用Comparator 40 Arrays.sort(a, 0, n, new cmp());//這個實現Comparator,就就跟c++中的sort函數調用就差不多了 41 42 for (int i = 0; i < n; i++) { 43 System.out.println(a[i].x + " " + a[i].y); 44 } 45 } 46 }
二、Comparable
強行對實現它的每個類的對象進行整體排序,實現此接口的對象列表(和數組)可以通過Collections.sort或Arrays.sort進行自動排序。就是輸入完了直接就默認排序了,
接口方法:
1 import java.util.*; 2 class structSort implements Comparable<structSort> { 3 4 String x; 5 int y; 6 public int compareTo(structSort o1) { 7 if (this.x.compareTo(o1.x) == 0) { 8 return this.y - o1.y; 9 } 10 return this.x.compareTo(o1.x); 11 } 12 } 13 14 public class Main { 15 public static void main(String[] args) { 16 Scanner cin = new Scanner(System.in); 17 int n = cin.nextInt(); 18 structSort a[] = new structSort[10]; 19 for (int i = 0; i < n; i++) { 20 a[i] = new structSort(); 21 a[i].x = cin.next(); 22 a[i].y = cin.nextInt(); 23 } 24 25 Arrays.sort(a, 0, n); 26 for (int i = 0; i < n; i++) { 27 System.out.println(a[i].x + " " + a[i].y); 28 } 29 } 30 }
acm中Java的應用
下面說一下ACM-ICPC隊員初用Java編程所遇到的一些問題:
1. 基本輸入輸出:
(1)
JDK 1.5.0 新增的Scanner類為輸入提供了良好的基礎,簡直就是為ACM-ICPC而設的。
讀一個整數: int n = cin.nextInt(); 相當於 scanf("%d", &n); 或 cin >> n;
讀一個字符串:String s = cin.next(); 相當於 scanf("%s", s); 或 cin >> s;
讀一個浮點數:double t = cin.nextDouble(); 相當於 scanf("%lf", &t); 或 cin >> t;
讀一整行: String s = cin.nextLine(); 相當於 gets(s); 或 cin.getline(...);
判斷是否有下一個輸入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble()
(3)
輸出一般可以直接用 System.out.print() 和 System.out.println(),前者不輸出換行,而後者輸出。
比如:System.out.println(n); // n 為 int 型
同一行輸出多個整數可以用
System.out.println(new Integer(n).toString() + " " + new Integer(m).toString());
也可重新定義:
static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
cout.println(n);
(4)
對於輸出浮點數保留幾位小數的問題,可以使用DecimalFormat類,
import java.text.*;
DecimalFormat f = new DecimalFormat("#.00#");
DecimalFormat g = new DecimalFormat("0.000");
double a = 123.45678, b = 0.12;
System.out.println(f.format(a));
System.out.println(f.format(b));
System.out.println(g.format(b));
這裏0指一位數字,#指除0以外的數字。
2. 大數字
BigInteger 和 BigDecimal 是在java.math包中已有的類,前者表示整數,後者表示浮點數
用法:
不能直接用符號如+、-來使用大數字,例如:
(import java.math.*) // 需要引入 java.math 包
BigInteger a = BigInteger.valueOf(100);
BigInteger b = BigInteger.valueOf(50);
BigInteger c = a.add(b) // c = a + b;
主要有以下方法可以使用:
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)
輸出大數字時直接使用 System.out.println(a) 即可。
3. 字符串
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類。
4. 調用遞歸(或其他動態方法)
在主類中 main 方法必須是 public static void 的,在 main 中調用非static類時會有警告信息,可以先建立對象,然後通過對象調用方法:
public class Main
{
...
void dfs(int a)
{
if (...) return;
...
dfs(a+1);
}
public static void main(String args[])
{
...
Main e = new Main();
e.dfs(0);
...
}
}
5. 其他註意的事項
(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://hi.baidu.com/oak_wesley/blog/item/35839200fd9dc10e1d9583de.html
Java進制轉換~集錦
由於Unicode兼容ASCII(0~255),因此,上面得到的Unicode就是ASCII。
java中進行二進制,八進制,十六進制,十進制間進行相互轉換
Integer.toHexString(int i)
十進制轉成十六進制
Integer.toOctalString(int i)
十進制轉成八進制
Integer.toBinaryString(int i)
十進制轉成二進制
Integer.valueOf("FFFF",16).toString()
十六進制轉成十進制
Integer.valueOf("876",8).toString()
八進制轉成十進制
Integer.valueOf("0101",2).toString()
二進制轉十進制
至於轉換成二進制或其他進制,Java API提供了方便函數,你可以查Java的API手冊。
以字符a的ASCII為例:
int i = ‘a‘;
String iBin = Integer.toBinaryString(i);//二進制
String iHex = Integer.toHexString(i);//十六進制
String iOct = Integer.toOctalString(i);//八進制
String iWoKao = Integer.toString(i,3);//三進制或任何你想要的35進制以下的進制
有什麽方法可以直接將2,8,16進制直接轉換為10進制的嗎?
java.lang.Integer類 parseInt(String s, int radix)
使用第二個參數指定的基數,將字符串參數解析為有符號的整數。
examples from jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
進制轉換如何寫(二,八,十六)不用算法
Integer.toBinaryString
Integer.toOctalString
Integer.toHexString
例一:
public class Test{
public static void main(String args[]){
int i=100;
String binStr=Integer.toBinaryString(i);
String otcStr=Integer.toOctalString(i);
String hexStr=Integer.toHexString(i);
System.out.println(binStr);
例二:
public classTestStringFormat {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: javaTestStringFormat <a number>");
System.exit(0);
}
Integer factor =Integer.valueOf(args[0]);
String s;
s =String.format("%d", factor);
System.out.println(s);
s = String.format("%x", factor);
System.out.println(s);
s = String.format("%o", factor);
System.out.println(s);
}
}
各種數字類型轉換成字符串型:
String s = String.valueOf( value); // 其中 value 為任意一種數字類型。
字符串型轉換成各種數字類型:
String s = "169";
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );
數字類型與數字類對象之間的轉換:
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i );
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
Java在ACM中的應用