ACM之java速成(轉)

來源:互聯網
上載者:User

這裡指的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(); // 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, 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的進位.


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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.