zoukankan      html  css  js  c++  java
  • Java在ACM中的应用

    输入:可以从文件中输入,也可以从控制台输入。

    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;
        }
    
    }
  • 相关阅读:
    Quartz学习——Spring和Quartz集成详解(三)
    Quartz学习——Quartz简单入门Demo(二)
    Quartz学习——Quartz大致介绍(一)
    Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
    使用spring+quartz配置多个定时任务
    quartz定时任务时间设置
    Hibernate事务与并发问题处理(乐观锁与悲观锁)
    Oracle体系结构之参数文件管理
    Oracle体系结构之密码文件管理
    LINUX RHEL AS 4 + ORACLE10G安装详解
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2689030.html
Copyright © 2011-2022 走看看