java learning

template


import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;
    long mod = 998_244_353L; // (long) 1e9 + 7 || (long) 1e9 + 9
    long inf = (long) (1e9 + 7);
    boolean multitests = true;

    private void solve() throws IOException {
        int n = in.nextInt();


    }  

    long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }
    long binpow(long a, long n) {
        long res = 1;
        a %= mod;
        n %= mod - 1;
        while (n > 0) {
            if (n % 2 == 1)
                res = (res * a) % mod;
            a = (a * a) % mod;
            n /= 2;
        }
        return res;
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s), 32768);
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = in.nextInt();
            return a;
        }

        int[][] nextIntTable(int n, int m) throws IOException {
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    a[i][j] = in.nextInt();
            return a;
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        for (int t = multitests ? in.nextInt() : 1; t-- > 0; )
            solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

图论

import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;
    long mod = 998_244_353L; // (long) 1e9 + 7 || (long) 1e9 + 9
    long inf = (long) (1e9+7);
    boolean multitests = true;

    private void solve() throws IOException {
        int n = in.nextInt();
        int m=in.nextInt();
        ArrayList<Edge> E = new ArrayList<Edge>();
        ArrayList<Integer>[] a=new ArrayList[n+1];
        int[] deg=new int[n+1];
        for (int i=1;i<=n;i++) a[i]=new ArrayList<Integer>();
        for(int i=1;i<=m;i++){
            int type=in.nextInt();
            int u=in.nextInt(),v=in.nextInt();
            E.add(new Edge(u,v));
            if (type==1){
                a[u].add(v);
                deg[v]++;
            }
        }

        //toposort
        Queue<Integer>Q=new LinkedList<Integer>();
        for (int i=1;i<=n;i++) if (deg[i]==0) Q.add(i);
        int cur=0;//check if there is a loop,
        int [] order=new int [n+1];
        while (!Q.isEmpty()){
            int now=Q.poll();
            cur++;
            order[now]=cur;
            for (int v:a[now]){
                deg[v]--;
                if (deg[v]==0) Q.add(v);
            }
        }
        if (cur!=n) out.println("NO");
        else{
            out.println("YES");
            for (Edge e:E){
                if (order[e.u]>order[e.v]){
                    out.println(e.v+" "+e.u);
                }else out.println(e.u+" "+e.v);
            }
        }

    }
    static class Edge{
        int u,v;
        Edge(int u,int v){
            this.u=u;
            this.v=v;
        }

    }


    int binSearch(int[] a, int x) {
        int l = -1, mid, r = a.length;
        while (l + 1 < r) {
            mid = (l + r) / 2;
            if (a[mid] >= x)
                r = mid;
            else
                l = mid;
        }
        return r;
    }

    void reverseInt(int[] a) {
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            int swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }

    void reverseLong(long[] a) {
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            long swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }

    int maxInt(int[] a) {
        int max = a[0];
        for (int i = 1; i < a.length; i++)
            if (max < a[i])
                max = a[i];
        return max;
    }

    long maxLong(long[] a) {
        long max = a[0];
        for (int i = 1; i < a.length; i++)
            if (max < a[i])
                max = a[i];
        return max;
    }

    int minInt(int[] a) {
        int min = a[0];
        for (int i = 1; i < a.length; i++)
            if (min > a[i])
                min = a[i];
        return min;
    }

    long minLong(long[] a) {
        long min = a[0];
        for (int i = 1; i < a.length; i++)
            if (min > a[i])
                min = a[i];
        return min;
    }

    long sumInt(int[] a) {
        long s = 0;
        for (int i = 0; i < a.length; i++)
            s += a[i];
        return s;
    }

    long sumLong(long[] a) {
        long s = 0;
        for (int i = 0; i < a.length; i++)
            s += a[i];
        return s;
    }

    long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    long binpow(long a, long n) {
        long res = 1;
        a %= mod;
        n %= mod - 1;
        while (n > 0) {
            if (n % 2 == 1)
                res = (res * a) % mod;
            a = (a * a) % mod;
            n /= 2;
        }
        return res;
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s), 32768);
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = in.nextInt();
            return a;
        }

        int[][] nextIntTable(int n, int m) throws IOException {
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    a[i][j] = in.nextInt();
            return a;
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        long[] nextLongArray(int n) throws IOException {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = in.nextLong();
            return a;
        }

        long[][] nextLongTable(int n, int m) throws IOException {
            long[][] a = new long[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    a[i][j] = in.nextLong();
            return a;
        }


        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        for (int t = multitests ? in.nextInt() : 1; t-- > 0; )
            solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

正常模板

import java.util.*;
public class ChoosingFlowers{
    static class Flower{
        private long happiness;
        private long continueHappiness;

        public Flower(Scanner in){
            this.happiness=in.nextLong();
            this.continueHappiness=in.nextLong();
        }

        public long getHappiness(){
            return happiness;
        }
        public long getContinueHappiness(){
            return continueHappiness;
        }
    }

    public long work(int n,int m,List<Flower> flowerList){
        flowerList.sort(Comparator.comparing(Flower::getHappiness).reversed());
        long ans=0L;
        long [] prefixsum=new long[m];
        prefixsum[0]=flowerList.get(0).happiness;
        for (int i=1;i<m;i++){
            prefixsum[i]=prefixsum[i-1]+flowerList.get(i).happiness;
        }
        for (int i=0;i<m;i++){
            Flower flower=flowerList.get(i);
            int l=0;
            int r=m-1;
            for (;l<=r;){
                int mid=(l+r)>>1;
                if (flowerList.get(mid).happiness>=flower.continueHappiness){
                    l=mid+1;
                }else{
                    r=mid-1;
                }
            }
            if (r>=n){
                ans=Math.max(ans,prefixsum[n-1]);
            }else if (r>=i){
                ans=Math.max(ans,prefixsum[r]+flower.continueHappiness*(n-r-1));
            }else if (r<0){
                ans=Math.max(ans,flower.happiness+flower.continueHappiness*(n-1));
            }else{
                ans=Math.max(ans,prefixsum[r]+flower.happiness+flower.continueHappiness*(n-r-2));
            }
        }
        return ans;
    }

    public static void main(String[] args){
        ChoosingFlowers choosingFlowers=new ChoosingFlowers();
        Scanner in=new Scanner(System.in);
        for (int t=in.nextInt();t>0;t--){
            int n=in.nextInt();
            int m=in.nextInt();
            List <Flower> flowerList=new ArrayList<>();
            for (int i=0;i<m;i++){
                flowerList.add(new Flower(in));
            }
            System.out.println(choosingFlowers.work(n,m,flowerList));
        }
        //in.close();
        //System.out.close();
    }
}

数组
ArrayList<Integer> ans = new ArrayList<>();
int [] a=new int[5];

排序
Collections.sort(a);
Collections.reverse(a);
Arrays.sort(a)



函数,set
public static int findMex(int[] arr) {
		HashSet<Integer> set = new HashSet<Integer>();
		for(int i = 0;i < arr.length;i++) {
			set.add(arr[i]);
		}
		
		for(int i = 0;i < arr.length;i++)
			if(!set.contains(i))return i;
		
		return arr.length;
	}



//print format
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);

        String firstName = scanner.next();
        scanner.next();
        scanner.next();
        scanner.next();
        String cuisine = scanner.next();

        System.out.println(String.format(
                "The form for %s is completed. We will contact you if we need a chef that cooks %s dishes.",
                firstName, cuisine));
    }
}


//scanner read, stack, system in out
import java.util.*;

class Main {
    public static void main(String[] args) {
        Stack<String> strings = new Stack<>();
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            strings.push(scanner.next());
        }

        while (!strings.empty()) {
            System.out.println(strings.pop());
        }
    }



java biginteger 大数二分

import java.math.BigInteger;
import java.util.Scanner;

public class hello2 {

static boolean check(BigInteger x){
    //System.out.println(x);
    //String tmp="1"+(String)"0".repeat(100);
    
    
    StringBuffer buf=new StringBuffer();
    buf.append("1");
    int clock=200;
    if(clock--!=0){
        buf.append("0");
    }
    String tmp=buf.toString();

    BigInteger l=new BigInteger("0"),r=x;

    //System.out.println(l+" "+r);

    while (l.compareTo(r)<=0){
        BigInteger mid=l.add(r);

        //System.out.println(mid);
        mid=mid.divide(BigInteger.valueOf(2));
        //System.out.println(mid);
        //return true;


        BigInteger midDouble=mid.multiply(mid);
        if (midDouble.equals(x)){
            return true;
        }
        else if (midDouble.compareTo(x)==-1){
            l=mid.add(BigInteger.valueOf(1));
        }
        else {
            r=mid.add(BigInteger.valueOf(-1));
        }
    }
    return false;
}

public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    int test=sc.nextInt();
    while (test--!=0){
        String n=sc.next();
        BigInteger N=new BigInteger(n);
        //System.out.println(N+" "+N.multiply(N.add(BigInteger.valueOf(-1))).divide(BigInteger.valueOf(2)));
        boolean result1=check(N);
        boolean result2=check(N.multiply(N.add(BigInteger.valueOf(-1))).divide(BigInteger.valueOf(2)));
        //System.out.println(result1+" "+result2);
        if (result1&&result2){
            System.out.println("Arena of Valor");
        }
        if (!result1&&result2){
            System.out.println("Clash Royale");
        }
        if (!result1&&!result2){
            System.out.println("League of Legends");
        }
        if (result1&&!result2){
            System.out.println("Hearth Stone");
        }
        //System.out.println("end");
    }
}

}













原文地址:https://www.cnblogs.com/reshuffle/p/13306300.html