Java在ACM中的應用

來源:互聯網
上載者:User

 

輸入:可以從檔案中輸入,也可以從控制台輸入。

import java.io.BufferedInputStream;import java.io.File;import java.util.Scanner;public class Demo {    public static void main(String[] args) throws Exception {        Scanner cin = null;        cin = new Scanner(new File("data.in"));// 從檔案讀取        // cin = new Scanner(new BufferedInputStream(System.in));// 從控制台輸入        int a = -1;        while (cin.hasNext()) {            a = cin.nextInt();            System.out.println(a);        }        cin.close();    }        }

   cin.nextType(); // 的某種類型的數,其中Type=Double,Float,Byte,Short,Int,Long,Boolean,BigInteger,BigDecimal
   cin.next();// 遇到空格返回,即得到一個字串
   cin.nextLine();// 遇到分行符號返回,即得到一行字串

應用最多的應是Java中的大數類:

BigInteger

    // 以下是BigInteger數的加、減、乘、除與取餘    public static BigInteger add(BigInteger a, BigInteger b) {        return a.add(b);    }    public static BigInteger subtract(BigInteger a, BigInteger b) {        return a.subtract(b);    }    public static BigInteger multiply(BigInteger a, BigInteger b) {        return a.multiply(b);    }    public static BigInteger divide(BigInteger a, BigInteger b) {        return a.divide(b);    }    public static BigInteger mod(BigInteger a, BigInteger b) {        return a.mod(b);    }

BigDecimal

    // 以下是BigDecimal數的加、減、乘、除    public static BigDecimal add(BigDecimal a, BigDecimal b) {        return a.add(b);    }    public static BigDecimal subtract(BigDecimal a, BigDecimal b) {        return a.subtract(b);    }    public static BigDecimal multiply(BigDecimal a, BigDecimal b) {        return a.multiply(b);    }    public static BigDecimal divide(BigDecimal a, BigDecimal b) {        return a.divide(b);    }

Arrays類中的應用:排序(sort),填充(fill),二分查詢(binarySearch)

排序:

int[] arr = new int[] { 12, -2, 3, -1, 22, 7 };        for (int i : arr) {            System.out.println(i);        }        Arrays.sort(arr);// 從小到大        System.out.println("---排序---");        for (int i : arr) {            System.out.println(i);        }                // result:        // 12        // -2        // 3        // -1        // 22        // 7        // ---排序---        // -2        // -1        // 3        // 7        // 12        // 22

 

參考型別排序:

import java.io.BufferedInputStream;import java.util.Iterator;import java.util.Scanner;import java.util.Set;import java.util.TreeSet;public class Demo {    public static void main(String[] args) throws Exception {        Scanner cin = new Scanner(new BufferedInputStream(System.in));        Set<Student> set = null;        Iterator<Student> ite = null;        Student stu = null;        String str = null;        String[] strStu = null;        int num = -1;        while (cin.hasNext()) {            str = cin.nextLine();            num = Integer.parseInt(str);            set = new TreeSet<Student>();            for (int i = 0; i < num; i++) {                str = cin.nextLine();                strStu = str.split(" ");                stu = new Student(Integer.parseInt(strStu[0]), strStu[1],                        Double.parseDouble(strStu[2]));                set.add(stu);            }            ite = set.iterator();            while (ite.hasNext()) {                stu = ite.next();                System.out.println(stu);            }        }        cin.close();    }    // result:    // 10    // 22 limin 90    // 12 limin 99    // 33 limin 78    // 9 zhaolin 100    // 0 zhaolin 90    // 2 zhaolin 99    // 88 wanzai 100    // 7 zhaolin 100    // 3 linmin 90    // 10 wanzai 99    // Student [sid=33, sname=limin, sscore=78.0]    // Student [sid=22, sname=limin, sscore=90.0]    // Student [sid=3, sname=linmin, sscore=90.0]    // Student [sid=0, sname=zhaolin, sscore=90.0]    // Student [sid=12, sname=limin, sscore=99.0]    // Student [sid=10, sname=wanzai, sscore=99.0]    // Student [sid=2, sname=zhaolin, sscore=99.0]    // Student [sid=88, sname=wanzai, sscore=100.0]    // Student [sid=7, sname=zhaolin, sscore=100.0]    // Student [sid=9, sname=zhaolin, sscore=100.0]}class Student implements Comparable<Student> {    private int sid;    private String sname;    private double sscore;    public int getSid() {        return sid;    }    public void setSid(int sid) {        this.sid = sid;    }    public String getSname() {        return sname;    }    public void setSname(String sname) {        this.sname = sname;    }    public double getSscore() {        return sscore;    }    public void setSscore(double sscore) {        this.sscore = sscore;    }    @Override    public String toString() {        return "Student [sid=" + sid + ", sname=" + sname + ", sscore="                + sscore + "]";    }    public Student(int sid, String sname, double sscore) {        super();        this.sid = sid;        this.sname = sname;        this.sscore = sscore;    }    // 先按分數從低到高排序,接著分數相同,按姓名排序,若姓名相同,按學號從小到大排序    @Override    public int compareTo(Student stu) {        int temp1 = Double.valueOf(this.sscore).compareTo(                Double.valueOf(stu.sscore));        if (0 == temp1) {            int temp2 = this.sname.compareTo(stu.sname);            if (0 == temp2) {                return Integer.valueOf(this.sid).compareTo(                        Integer.valueOf(stu.sid));            }            return temp2;        }        return temp1;    }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.