头像

MJ.Smith

生命绽放于战场,璀璨,却仅限于你的眼中。




离线:11小时前


最近来访(36)
用户头像
潘潘_the_panda
用户头像
Nan97
用户头像
收拾
用户头像
心升明月
用户头像
所見即我
用户头像
LKS
用户头像
github_smallzhong
用户头像
该用户被封禁
用户头像
不吃糖的Au
用户头像
cdlyk
用户头像
Misaka_9982
用户头像
我想要桃
用户头像
菜本菜
用户头像
Peter_5
用户头像
maro
用户头像
1900-
用户头像
cht
用户头像
伍六柒_6
用户头像
l1l1s
用户头像
张某某是谁啊

活动打卡代码 AcWing 1210. 连号区间数


import java.io.* ;
import java.util.* ;
public class Main{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in) ;
        int p = in.nextInt() ;
        int arr[] = new int[p + 1] ;
        for(int i = 1 ; i <= p ; i ++){
            arr[i] = in.nextInt() ;
        }
        int cnt = 0 ;
        for(int i = 1 ; i <= p ; i ++){
            int min = arr[i] ,max = arr[i] ;

            for(int j = i ; j <= p ; j ++){
                min = Math.min(min,arr[j]) ;
                max = Math.max(max,arr[j]) ;
                if(max == min + j - i) cnt ++ ;
            }

        }System.out.println(cnt) ;
    }
}




字符串转字符数组

public static void main(String[] args){
        Scanner in = new Scanner(System.in) ;
        String s1 = in.next() ;
        char c1[] = s1.toCharArray() ;//字符串转字符数组
        for(int i = 0 ; i < c1.length ; i ++){
            System.out.println(c1[i]) ;
        }
}

input :

abcdefg

output :

a
b
c
d
e
f
g

整行读取并分割

public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in)) ;
        int m = Integer.parseInt(in.readLine()) ;//readLine()读取整行内容,遇到空格或回车终止读入;Integer.parseInt()用于将字符串转成数字(int)
        String s[] = in.readLine().split(" ") ; 
        System.out.println(m) ;
        for(int i = 0 ; i < s.length ; i ++){//字符串长度(.length());数组长度(.length)
            System.out.print(s[i] + " ") ;
        }
}

input :

5
abc efg

output :

5
abc efg

进制转换

public static void main(String[] args){
        int num=1234;
        System.out.print("0"+Integer.toOctalString(num)+" ");
        System.out.println("0X"+Integer.toHexString(num).toUpperCase());
        int i = 1234 ;
        System.out.println(Integer.toBinaryString(i));  //返回i的二进制的字符串表示
        System.out.println(Integer.toOctalString(i));   //返回i的八进制的字符串表示
        System.out.println(Integer.toHexString(i));     //返回i的十六进制的字符串表示
        System.out.println(Integer.toString(i, 16));    //返回i的p进制的字符串表示
        // System.out.println(Integer.toString(i, p).toUpperCase());   
        //返回i的16进制的字符串表示且字母为大写字母(默认为小写字母)
}



import java.io.* ;
import java.util.*;
import java.lang.Math ;

public class Main{
    static int n ;
    static int arr[] = new int[1110] ;
    static int dp[] = new int[1110] ;
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)) ;
    public static void main(String[] args) throws IOException{
        n = in.nextInt() ;
        for(int i = 0 ; i < n ; i ++){
            arr[i] = in.nextInt() ;
        }
        int res = 0 ;
        for(int i = 0 ; i < n ; i ++){
            dp[i] = 1 ;
            for(int j = 0 ; j < i ; j ++){
                if(arr[j] < arr[i]) dp[i] = Math.max(dp[i],dp[j] + 1) ;
            }
        }
        for (int i = 1; i <= n; i++) {
            res = Math.max(res, dp[i]);
        }
        System.out.println(res);

    }
}


活动打卡代码 AcWing 1015. 摘花生

import java.util.* ;
import java.io.* ;
import java.lang.Math;

public class Main{
    static int T , R , C ;
    // static int w[] = new int[10000] ;
    // static int v[] = new int[10000] ;
    static int m[][] = new int[1010][1010] ;
    static int dp[][] = new int[1010][1010] ;//f[i,j]从前i个物体中选,且总体积不超过j的所有方案的集合
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)) ;
    public static void main(String[] args) throws IOException{
        T = in.nextInt() ;
        while(T -- > 0){
            R = in.nextInt() ;
            C = in.nextInt() ;
            for(int i = 1 ; i <= R ; i ++){
                for(int j = 1 ; j <= C ; j ++){
                    m[i][j] = in.nextInt() ;
                }
            }
            for(int i = 1 ; i <= R ; i ++){
                for(int j = 1 ; j <= C ; j ++){
                    dp[i][j] = Math.max(dp[i][j-1] + m[i][j] , dp[i-1][j] + m[i][j]) ;
                }
            }
            System.out.println(dp[R][C]) ;
        }
    }
}


活动打卡代码 AcWing 2. 01背包问题

import java.util.* ;
import java.io.* ;
import java.lang.Math;

public class Main{
    static int N , V , res ;
    static int w[] = new int[10000] ;
    static int v[] = new int[10000] ;
    static int f[][] = new int[1010][1010] ;//f[i,j]从前i个物体中选,且总体积不超过j的所有方案的集合
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)) ;
    public static void main(String[] args) throws IOException{
        N = in.nextInt() ;//f[i-1][j]
        V = in.nextInt() ;//f[i-1][j-v[i]]+w[i]
        for(int i = 1 ; i <= N ; i ++){
            v[i] = in.nextInt() ;
            w[i] = in.nextInt() ;
        }
        for(int i = 1 ; i <= N ; i ++){
            for(int j = 1 ; j <= V ; j ++){
                if(j >= v[i]) {
                    f[i][j] = Math.max(f[i-1][j],f[i-1][j-v[i]]+w[i]) ;
                }
                else f[i][j] = f[i-1][j] ;
            }
        }
        System.out.println(f[N][V]) ;
    }
}


活动打卡代码 AcWing 1211. 蚂蚁感冒

import java.util.* ;
import java.lang.Math;

public class Main{
    static int n , m , cnt ;
    static int[] arr = new int[100] ;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in) ;
        n = in.nextInt() ;
        for(int i = 0 ; i < n ; i ++){
            arr[i] = in.nextInt() ;
        }
        int l = 0 , r = 0 ;
        for(int i = 1  ; i < n ; i ++){
            if(Math.abs(arr[i]) < Math.abs(arr[0]) && arr[i] > 0) l ++ ;
            else if(Math.abs(arr[i]) > Math.abs(arr[0]) && arr[i] < 0) r ++ ;
        }
        if(arr[0] > 0 && r == 0 || arr[0] < 0 && l == 0) System.out.println(1) ;
        else System.out.println(l + r + 1) ;
    }
}


活动打卡代码 AcWing 1216. 饮料换购

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

public class Main{
    static int n , m ;
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)) ;
    public static void main(String[] args) throws IOException {
        n = in.nextInt() ;
        m = n ;
        while(n / 3 != 0){
            m += n / 3 ;       //133
            n = n / 3 + n % 3 ;
        }
        System.out.println(m);
    }
}



哪位大佬知道为什么这种写法什么也不输出呢?(可以成功编译)

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

public class Main{
    private static int N = 100010;
    private static int[] q = new int[N];
    private static int tt = -1;
    private static int hh = 0;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    public static void main(String[] args) throws IOException{
        int n = in.nextInt() ;
        while(n-->0){
            String str = in.next() ;
            if(str == "push"){
                int x = in.nextInt() ;
                q[++ tt] = x ;
            }else if(str == "pop"){
                if(tt != -1) hh ++ ;
            }else if(str == "query"){
                // System.out.println(q[hh]) ;
                out.write(q[hh] + "\n");
            }else if(str == "empty"){
                if(tt < hh){
                    // System.out.println("YES");
                    out.write("YES" + "\n");
                }else{
                    // System.out.println("NO");
                    out.write("NO" + "\n");
                }
            }
            // System.out.println(str) ;

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

}


活动打卡代码 AcWing 1205. 买不到的数目

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

public class Main{
    static int n , m ;
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)) ;
    public static void main(String[] args) throws IOException {
        n = in.nextInt() ;
        m = in.nextInt() ;
        int t = (n-1)*(m-1) - 1 ;
        System.out.print(t) ;

    }
}


活动打卡代码 AcWing 796. 子矩阵的和

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

public class Main{
    static int N = 1010; 
    static int n , m ,q ;
    static int arr[][] = new int[N][N];
    static int s[][] = new int[N][N];
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    static Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))) ;
    public static void main(String[] args) throws IOException{
        n = in.nextInt() ;
        m = in.nextInt() ;
        //n行m个整数
        q = in.nextInt() ;
        for(int i = 1 ; i <= n ; i ++) 
        {
            for(int j = 1 ; j <= m ; j ++){
                int t = in.nextInt() ;
                arr[i][j] = t ;
            }
        }

        for(int i = 1 ; i <= n ; i ++) 
        {
            for(int j = 1 ; j <= m ; j ++){
                s[i][j] = s[i-1][j] + s[i][j-1] + arr[i][j] - s[i-1][j-1] ;

            }
            // System.out.println();
        }
        while(q-- > 0)
        {
            int x1 = in.nextInt() ;
            int y1 = in.nextInt() ;
            int x2 = in.nextInt() ;
            int y2 = in.nextInt() ;
            // out.write(s[r] - s[l-1]) ;
            // out.write("\n");
            System.out.println(s[x2][y2] + s[x1-1][y1-1] - s[x1-1][y2] - s[x2][y1-1]) ;
        }
        // out.flush();
    }

}