Java 刷题之旅(51NOD基础题目)

51Nod - 1000 A + B

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
    	Scanner cin = new Scanner(System.in);
    	int a = cin.nextInt();
    	int b = cin.nextInt();
    	System.out.println(a + b);
    }
}

—————————————————————————————————————————————————————

51Nod - 1005 大数加法

//package text;

import java.util.*;
import java.math.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) {
    	Scanner cin = new Scanner(System.in);
    	BigInteger a = cin.nextBigInteger();
    	BigInteger b = cin.nextBigInteger();
    	BigInteger c = a.add(b);
    	System.out.println(c);
    }
}

—————————————————————————————————————————————————————

51Nod - 1006 最长公共子序列Lcs

import java.util.*;

public class Main{
    static int[][] dp = new int[1010][1010];
    static int[][] pos = new int[1010][1010];
    static String a = "!", b = "#", str = new String();

    public static void Print(int i, int j){
        if(i == -1 || j == -1) return;
        if(pos[i][j] == 1){
            Print(i-1, j-1);
            System.out.print(a.charAt(i));
        }
        else if (pos[i][j] == 2) Print(i-1, j);
        else Print(i, j-1);
    }

    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        str = cin.nextLine();
        a += str;
        str = cin.nextLine();
        b += str;
        int l1 = a.length();
        int l2 = b.length();
        for(int i = 1; i < l1; i++){
            for(int j = 1; j < l2; j++){
                if(a.charAt(i) == b.charAt(j)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                    pos[i][j] = 1;
                }
                else{
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                    if(dp[i-1][j] > dp[i][j-1]) pos[i][j] = 2;
                    else pos[i][j] = 3;
                }
            }
        }
        Print(l1-1, l2-1);
    }
}

 

—————————————————————————————————————————————————————

51Nod - 1008 N的阶乘 mod P(同余定理)

import java.awt.print.PrinterGraphics;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        long n = cin.nextLong();
        long p = cin.nextLong();
        long ans = 1;
        for(int i = 2; i <= n; i++){
            ans = ans * i % p;
        }
        ans %= p;
        System.out.println(ans);
    }
}

 

—————————————————————————————————————————————————————

51Nod - 1011 最大公约数GCD

import java.awt.print.PrinterGraphics;
import java.util.*;

public class Main{
    public static long Gcd(long a, long b){
        return b == 0 ? a : Gcd(b, a % b);
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        long a = cin.nextLong();
        long b = cin.nextLong();
        System.out.println(Gcd(a, b));
    }
}

—————————————————————————————————————————————————————

51Nod - 1012 最小公倍数LCM

import java.util.*;

public class Main{
    public static long Gcd(long a, long b){
        return b == 0 ? a : Gcd(b, a % b);
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        long a = cin.nextLong();
        long b = cin.nextLong();
        System.out.println(a * b / Gcd(a, b));
    }
}

—————————————————————————————————————————————————————

51Nod - 1018 排序(sort)

import java.lang.reflect.Array;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i ++)
            a[i] = cin.nextInt();
        Arrays.sort(a);
        for (int i = 0; i < n; i ++)
            System.out.println(a[i]);
    }
}

—————————————————————————————————————————————————————

51Nod - 1019 逆序数

 

—————————————————————————————————————————————————————

51Nod - 1027 大数乘法

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main{

    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger a = cin.nextBigInteger();
        BigInteger b = cin.nextBigInteger();
        BigInteger c = a.multiply(b);
        System.out.println(c);
    }
}

—————————————————————————————————————————————————————

51Nod - 1046 A^B Mod C(快速幂取模)

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main{
    public static long qkm(long a, long b, long c){
        long base = 1;
        while(b > 0){
            a = a % c;
            if((b & 1) == 1) base = base * a % c;
            b = b >> 1;
            a = a * a % c;
        }
        return base;
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        long a = cin.nextLong();
        long b = cin.nextLong();
        long c = cin.nextLong();
        long ans = qkm(a, b ,c);
        System.out.println(ans);
    }
}

—————————————————————————————————————————————————————

51Nod - 1049 最大子段和(DP)

import java.lang.reflect.Array;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        long[] a = new long[n+1];
        for(int i = 1; i <= n; i ++)
            a[i] = cin.nextLong();
        long[] dp = new long[n+1];
        long maxx = 0;
        for(int i = 1; i <= n; i++){
            dp[i] = Math.max(dp[i-1] + a[i], a[i]);
            maxx = Math.max(dp[i], maxx);
        }
        System.out.println(maxx);
    }
}

—————————————————————————————————————————————————————

51Nod - 1057 N的阶乘

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        BigInteger ans = BigInteger.valueOf(1);
        for(int i = 2; i <= n; i++){
            ans = ans.multiply(BigInteger.valueOf(i));
        }
        System.out.println(ans);
    }
}

—————————————————————————————————————————————————————

51Nod - 1058 N的阶乘的长度

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        double Pi = Math.acos(-1.0);
        double e = 2.718281828459;
        int n = cin.nextInt();
        int ans = 0;
        if(n == 1) ans = 1;
        else ans = (int)(0.5 * Math.log10(2.0*Pi*n) + n* Math.log10(n*1.0/e) + 1);
        System.out.println(ans);
    }
}

—————————————————————————————————————————————————————

51Nod - 1066 Bash游戏

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        while(T-- > 0){
            int n = cin.nextInt();
            int k = cin.nextInt();
            if(n % (k+1) == 0) System.out.println("B");
            else System.out.println("A");
        }
    }
}

—————————————————————————————————————————————————————

51Nod - 1069 Nim游戏

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int ans = 0;
        for(int i = 0 ; i < n; i++){
            int x = cin.nextInt();
            ans = ans ^ x;
        }
        if(ans == 0) System.out.println("B");
        else System.out.println("A");
    }
}

—————————————————————————————————————————————————————

51Nod - 1072 威佐夫游戏

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        while(T-- > 0){
            int x = cin.nextInt();
            int y = cin.nextInt();
            if(x > y) {
                int t = x;
                x = y;
                y = t;
            }
            int temp = (int)(Math.floor((y-x) * (1+Math.sqrt(5.0))/2.0));
            if(temp == x) System.out.println("B");
            else System.out.println("A");
        }
    }
}

—————————————————————————————————————————————————————

51Nod - 1073 约瑟夫环

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int ans = 0;
        int n = cin.nextInt();
        int k = cin.nextInt();
        for(int i = 2; i <= n; i++)
            ans = (ans + k) % i;
        System.out.println(ans + 1);
    }
}

—————————————————————————————————————————————————————

51Nod - 1088 最长回文子串

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

public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        String str = cin.nextLine();
        int len = str.length();
        char[] s = str.toCharArray();
        int[] a = new int[len];
        int[] b = new int[len];
        int maxx = -1;
        for(int i = 0; i < len; i++){
            a[i]++;
            int j = i - 1, k = i + 1;
            while(j >= 0 && k < len){
                if(s[j] == s[k]) a[i] += 2;
                else break;
                j--; k++;
            }
            int w = i, l = i + 1;
            while(w >= 0 && l < len) {
                if(s[w] == s[l]) b[i] += 2;
                else break;
                w--; l++;
            }
        }
        Arrays.sort(a);
        Arrays.sort(b);
        System.out.printf("%d\n", Math.max(a[len-1], b[len-1]));
    }
}

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

 

—————————————————————————————————————————————————————

你可能感兴趣的:(Java,学习)