Java学习总结

1.结构体排序

import java.util.*;

public class Main {
    
    static class Node{   //内部静态类
        String s;
        int x;
    }
    static class compareTo implements Comparator{  //升序排序
        public int compare(Node a,Node b){
            if(a.x>b.x) return 1;
            else if(a.x==b.x) return 0;
            else return -1;
        }
    }
//    static class compareTo implements Comparator{
//        public int compare(Object a, Object b){
//            Node t1 = (Node) a;
//            Node t2 = (Node) b;
//            if(t1.x > t2.x) return 1;
//            else if(t1.x == t2.x) return 0;
//            else return -1;
//        }
//    }
    static Scanner sc = new Scanner(System.in);
    static Node[] s = new Node[105];
    
    public static void main(String[] args){
        
        int n = sc.nextInt();
        for(int i = 1; i <= n; i++){
            s[i] = new Node();
            s[i].s = sc.next();
            s[i].x = sc.nextInt();
        }
        Arrays.sort(s, 1, n+1, new compareTo());
        for(int i = 1; i <= n; i++){
            if(i > 1) System.out.print(" ");
            System.out.print(s[i].s + " " + s[i].x);
        }
        System.out.println();
    }
} 
     
    结构体排序 
    
   

 

2.循环式并查集

static int[] f = new int[100005];
static int find(int x) {    //循环式路径压缩
    int p=x;
    while(f[p]!=p){
        p=f[p];
    }
    while(x!=p){
        int t=f[x];
        f[x]=p;
        x=t;
    }
    return x;
}
循环式并查集

 

3.矩阵快速幂

import java.util.Scanner;

public class Main {
    
    static final long MOD = 1010101010;
    static Scanner sc = new Scanner(System.in);
    static class mat{
        long[][] a = new long[10][10];
    }
    static mat mul(mat x,mat y) {
        mat a = new mat();
        for(int i=1;i<=2;i++) {
            for(int j=1;j<=2;j++) {
                for(int k=1;k<=2;k++) {
                    a.a[i][j]+=x.a[i][k]*y.a[k][j]%MOD;
                    a.a[i][j]%=MOD;
                }
            }
        }
        return a;
    }
    static long qMod(mat a,long n) {
        mat t = new mat();
        t.a[1][1]=1;t.a[1][2]=1;
        t.a[2][1]=1;t.a[2][2]=0;
        while(n>0) {
            if(n%2==1) a=mul(t,a);
            n>>=1;
            t=mul(t,t);
        }
        return a.a[1][1];
    }
    public static void main(String[] args) {
        
        long n=sc.nextLong();
        if(n==1) {
            System.out.println(1);
        }
        else if(n==2) {
            System.out.println(2);
        }
        else {
            mat a = new mat();
            a.a[1][1]=2;
            a.a[2][1]=1;
            System.out.println(qMod(a,n-2));
        }
    }
}
矩阵快速幂

 

4.结构体队列陷阱

import java.util.ArrayDeque;

public class Main {

    static class Node{
        int x,y;
        public Node(int x,int y) {  //每次新建一个对象存值
            this.x=x;
            this.y=y;
        }
    }
    static ArrayDeque q = new ArrayDeque();
    
    public static void main(String[] args) {

//        Node no = new Node();
//        no.x=1;no.y=1;
//        q.offerLast(no);
//        no = new Node();  //没有这句会对第一个值进行修改
//        no.x=2;no.y=2;
//        q.offerLast(no);
        q.offerLast(new Node(1,1));   //规范写法
        q.offerLast(new Node(2,2));
        while(q.size()>0) {
            System.out.println(q.peekFirst().x);
            System.out.println(q.peekFirst().y);
            q.pollFirst();
        }
    }

}
结构体队列陷阱

 

5.优先队列

import java.util.Comparator;
import java.util.PriorityQueue;

class VowelComparator implements Comparator {

    @Override
    public int compare(String x, String y) {
        if (getVowelCount(x) < getVowelCount(y)) {
            return -1;
        } else if (getVowelCount(x) > getVowelCount(y)) {
            return 1;
        }
        return 0;
    }

    public int getVowelCount(String word) {
        int vowel = 0;
        for (int i = 0; i < word.length(); i++) {
            char chr = word.charAt(i);
            if (chr == 'a' || chr == 'A' || chr == 'e' || chr == 'E'
                    || chr == 'i' || chr == 'I' || chr == 'o' || chr == 'O'
                    || chr == 'u' || chr == 'U')
                vowel++;
        }
        return vowel;
    }
}
public class Main {
    public static void main(String[] args) {
        Comparator queueComparator = new VowelComparator();
        PriorityQueue priorityQueue = new PriorityQueue(10,
                queueComparator);
        priorityQueue.add("orange");
        priorityQueue.add("fig");
        priorityQueue.add("watermelon");
        priorityQueue.add("lemon");
        while (priorityQueue.size() != 0) {
            System.out.println(priorityQueue.remove());
        }
    }
}
优先队列

 

6.进制转换

进制转换

 

7.输入挂

输入挂

 

8.map映射

import java.util.*;

public class Main {
    
    static Scanner sc = new Scanner(System.in);
    static int[] a = new int[105];
    
    public static void main(String[] args){
        
        int n,i;
        String key, maxk;
        int value, maxv;
        
        while(sc.hasNext()){    
            n=sc.nextInt();
            if(n == 0) break;
            Map map = new HashMap();
            Set set = map.keySet();
            for(i = 1; i <= n; i++){
                key = sc.next();
                if(map.get(key)==null) value = 1;
                else{
                    value = (int) map.get(key);
                    value++;
                }
                map.put(key, value);
            }
            Iterator iter = set.iterator();
            maxv = 0;maxk = null;
            while(iter.hasNext()){
                key = (String) iter.next();
                value = (int) map.get(key);
                if(value > maxv){
                    maxk = key;
                    maxv =value;
                }
            }
            System.out.println(maxk);
        }
    }
}
map映射

 

9.宏定义

static final int MAX = 1005;

 

10.Set的使用

public class Demo4 {  
    public static void main(String[] args) {  
        //Set 集合存和取的顺序不一致。  
        Set hs = new HashSet();  
        hs.add("世界军事");  
        hs.add("兵器知识");  
        hs.add("舰船知识");  
        hs.add("汉和防务");  
        System.out.println(hs);  
        // [舰船知识, 世界军事, 兵器知识, 汉和防务]  
        Iterator it = hs.iterator();  
        while (it.hasNext()) {  
            System.out.println(it.next());  
        }  
    }  
}  
Set的使用

 

11.程序计时

long startTime=System.currentTimeMillis();//记录开始时间  
method();//此处为你调用的方法  
long endTime=System.currentTimeMillis();//记录结束时间  
double excTime=(double)(endTime-startTime)/1000;  
System.out.println("执行时间:"+excTime+"s");
程序计时

 

 

以下博客对java在ACM中的总结非常好,link一下。

ttp://www.cppblog.com/vontroy/archive/2010/05/24/116233.html

www.xuebuyuan.com/zh-hant/977823.html

https://blog.csdn.net/hnshhslsh/article/details/53159283

转载于:https://www.cnblogs.com/yzm10/p/7527637.html

你可能感兴趣的:(java)