由於java里面有一些東西比c/c++方便(尤其是大數據高精度問題,備受廣大ACMer歡迎),所以就可以靈活運用這三種來實現編程,下面是我自己在各種大牛那里總結了一些,同時加上自己平時遇到的一些java上面的東西,像結構體排序什么的都有添加進去,博客一直會在更新,對初學者還是有一些幫助的,大牛們就可以忽略了,如果博客有什么問題,歡迎指出!
java中的輸出a+b
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner (System.in);
while(in.hasNext())
{
int a,b;
a=in.nextInt();
b=in.nextInt();
System.out.println(a+b);
}
}
}
這里指的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是不可以修改的,要修改只能轉換為字符數組.
例程:
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)); // st.charAt(i)就相當於st[i].
char [] ch;
ch = st.toCharArray(); // 字符串轉換為字符數組.
for (i = 0; i < ch.length; i++) ch[i] += 1;
System.out.println(ch); // 輸入為“bcdefgh”.
if (st.startsWith("a")) // 如果字符串以'0'開頭.
{
st = st.substring(1); // 則從第1位開始copy(開頭為第0位).
}
}
}
4. 高精度
BigInteger和BigDecimal可以說是acmer選擇java的首要原因。
函數:add, subtract, multiply,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); // 把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
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);
}
}
}
//End by abilitytao
6. 排序
函數:Arrays.sort();
例程:
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. 結構體排序:
例子:一個結構體有兩個元素String x,int y,排序,如果x相等y升序,否者x升序。
一、Comparator
強行對某個對象collection進行整體排序的比較函數,可以將Comparator傳遞給Collections.sort或Arrays.sort。
接口方法:這里也給出了兩種方法,
import java.util.*;
class structSort{
String x;
int y;
}
class cmp implements Comparator<structSort>{
public int compare(structSort o1, structSort o2) {
if(o1.x.compareTo(o2.x) == 0){//這個相當於c/c++中strcmp(o1.x , o2,x)
return o1.y - o2.y;
}
return o1.x.compareTo(o2.x);
}
}
public class Main {
public static void main(String[] args) {
Comparator<structSort> comparator = new Comparator<structSort>(){
public int compare(structSort o1, structSort o2) {
if(o1.x.compareTo(o2.x) == 0){
return o1.y - o2.y;
}
return o1.x.compareTo(o2.x);
}
};
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
structSort a[] = new structSort[10];
for (int i = 0; i < n; i++) {
a[i] = new structSort();
a[i].x = cin.next();
a[i].y = cin.nextInt();
}
Arrays.sort(a,0,n,comparator);//這個直接使用Comparator
Arrays.sort(a,0,n,new cmp());//這個實現Comparator,就就跟c++中的sort函數調用就差不多了
for (int i = 0; i < n; i++) {
System.out.println(a[i].x+" "+a[i].y);
}
}
}
二、Comparable
強行對實現它的每個類的對象進行整體排序,實現此接口的對象列表(和數組)可以通過Collections.sort或Arrays.sort進行自動排序。就是輸入完了直接就默認排序了,
接口方法:
import java.util.*;
class structSort implements Comparable<structSort>{
String x;
int y;
public int compareTo(structSort o1) {
if(this.x.compareTo(o1.x) == 0){
return this.y - o1.y;
}
return this.x.compareTo(o1.x);
}
}
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
structSort a[] = new structSort[10];
for (int i = 0; i < n; i++) {
a[i] = new structSort();
a[i].x = cin.next();
a[i].y = cin.nextInt();
}
Arrays.sort(a,0,n);
for (int i = 0; i < n; i++) {
System.out.println(a[i].x+" "+a[i].y);
}
}
}
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();