Java在acm里的基础操作

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

/**
 * Copyright (C), 2018-2018, csust
 * FileName: hhd
 * Author:   Cwolf9
 * Date:     2018/13/32 25:61
 */

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.math.*;
import java.util.Arrays;

public class hhd{
    static int N = (int)1e5+7;
    static PrintStream putOut = System.out;//可以换一种写法~

    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);//读入

        StringBuilder sb;//String与StringBuilder
        String tmp = new String();
        tmp = cin.nextLine();
        sb = new StringBuilder(tmp);
        sb.setCharAt(1,'3');//首位是第0位,改变第1位为'3'
        tmp = sb.toString();
        System.out.println(tmp+" "+sb);

        BigInteger bigNum = new BigInteger(tmp);
        System.out.println(bigNum.multiply(BigInteger.valueOf(2)));//大整数运算
        String a = bigNum.toString();//两种进制转化的方法
        //a = new BigInteger(a,10).toString(2);
        a=bigNum.toString(2);
        System.out.println(a);


        int AA = 199;//控制输出格式
        double b = 9.9999;
        System.out.printf("%04d %.5f\n",AA,b);
        System.out.format("%04d %.5f\n",AA,b);
        System.out.printf("%.3f\n",(double)AA);
        DecimalFormat p3 = new DecimalFormat("#.000#");
        System.out.println(p3.format(AA));
        bigNum = cin.nextBigInteger();
        System.out.printf("%d\n",bigNum);

        String str;//进制转化
        int A=8,B=2;
        str = Integer.toString(A,2);//把int型数据转换成X进制数并转换成String型
        System.out.println(str);
        A = Integer.parseInt(str,2);//把字符串当作X进制数转换成10进制int型
        System.out.println(A);

        str = "aryawu";//String与字符串
        char[] ch = str.toCharArray();
        System.out.println(ch.length+str.substring(2,4));

        int temp = -1;//位运算
        System.out.println((temp>>1)+" "+(temp>>>1));
        System.out.println(Integer.toString(31,16));

        //数组的一些操作
        int[] ar = {2,5,9,3,34,1,2};
        boolean[] vis = new boolean[100];
        boolean[] Vis = new boolean[100];
        Arrays.sort(ar);
        Arrays.fill(vis,true);
        putOut.println("相同?:"+Arrays.equals(vis,Vis));
        putOut.println(Arrays.binarySearch(ar,5));


        //数据结构
        Map map=new HashMap();//以下的俩种使用方式都是可以的
        map.put("a",1);
        map.put(11,"abc");
        Map<String,Integer> mp=new HashMap<String,Integer>();
        mp.put("a",1);mp.put("as",2);mp.put("b",3);
        mp.put("a",5);
        mp.remove("a");
        mp.get("as");
        //获取所有的key和value
        for(Map.Entry<String, Integer> entry : mp.entrySet()){
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
        }
        //获取所有的key
        Set<String> keys = mp.keySet();
        for(String key: keys){
            System.out.println(key);
        }
        //获取所有的value
        Collection<Integer> values = mp.values();
        for(Integer value: values){
            System.out.println(value);
        }
        putOut.println("===============");

        Queue<Integer> que = new LinkedList<Integer>();
        /*
        * 建议使用offer而不使用add
        * 建议使用poll而不使用remove
        * 建议使用peek而不使用element
        * 因为前者不会抛出异常,会有返回值
        * */
        que.offer(5);que.offer(2);
        putOut.println(que.poll());
        que.peek();
        for(int i:que){//遍历队列
            putOut.println(i);
        }
        putOut.println("===============");

        Stack<Integer>st = new Stack<>();
        st.push(12);st.push(23);
        st.peek();
        st.pop();
        st.empty();
        putOut.println(st.search(23));
        putOut.println(st.search(12));


        //数组和List的转换
        List list = new ArrayList();
        list.add("a");
        list.add("12");
        int len = list.size();
        String[] arr_tmp = new String[len+1];
        for(int i=0;i<len;++i){
            arr_tmp[i]=(String)list.get(i);
            //arr_tmp[i]=list.get(i).toString();
        }

        List<String> list1 = new ArrayList<String>();
        list1.add("a");
        list1.add("12");
        String[] arr_tmp1 = (String[])list1.toArray(new String[len]);

        List<String>list2 = Arrays.asList(arr_tmp);
        for(int i=0;i<list2.size();++i){
            putOut.println(list2.get(i));
        }

        List<String> list3 = Arrays.asList("12","34","56");
        for(int i=0;i<list3.size();i++){
            System.out.println(list3.get(i));
        }

        while (cin.hasNext()){//多组输入
            int n = cin.nextInt();
            System.out.println(Math.pow(n,4));
        }

    }
}


优先队列

/**
 * Copyright (C), 2018-2018, csust
 * FileName: hhd
 * Author:   Cwolf9
 * Date:     2018/13/32 25:61
 */
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;


public class fun {
    private String name;
    private int population;

    public fun(String name, int population) {
        this.name = name;
        this.population = population;
    }

    public String getName() {
        return this.name;
    }

    public int getPopulation() {
        return this.population;
    }

    public String toString() {
        return getName() + "-" + getPopulation();

    }

    public static void main(String[] args) {
        Comparator<fun> OrderIsdn = new Comparator<fun>() {
            public int compare(fun o1, fun o2) {
                int numbera = o1.getPopulation();
                int numberb = o2.getPopulation();
                if (numberb > numbera) {
                    return 1;
                }else if (numberb < numbera) {
                    return -1;
                } else {
                    return 0;
                }
            }
        };

        Queue<fun> priorityQueue=new PriorityQueue<fun>(11,OrderIsdn);

        fun t1=new fun("t1",1);
        fun t2=new fun("t2",2);

        fun t3=new fun("t3",3);
        fun t4=new fun("t4",0);
        priorityQueue.add(t1);
        priorityQueue.add(t2);
        priorityQueue.add(t3);
        priorityQueue.add(t4);

        System.out.println(priorityQueue.poll().toString());

    }

}

猜你喜欢

转载自blog.csdn.net/qq_39599067/article/details/80765766