头像

LaLa_JUROU




离线:31分钟前


最近来访(2)
用户头像
明_43
用户头像
种族

活动打卡代码 AcWing 3498. 日期差值

LaLa_JUROU
18小时前
import java.io.*;
public class Main{
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    static String date1,date2;
    public static void main(String[] args) throws IOException{
        while((date1 = in.readLine()) != null && (date2 = in.readLine()) != null) {
            System.out.println(Math.abs(getDays(date1) - getDays(date2)) + 1);
        }
    }
    static long getDays(String date) {
        long sum = 0;
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(4, 6));
        int day = Integer.parseInt(date.substring(6, 8));
        //年计算天
        for(int i = 1; i < year; i ++) {
            if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)){//判断是否闰年
                sum += 366;
            } else {
                sum += 365;
            }
        }
        //月计算天
        for(int i = 1; i < month; i ++) {
            if(i == 4 || i == 6 || i == 9 || i == 11) {
                sum += 30;
            } else if (i == 2) {
                if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){//判断是否闰年
                    sum += 29;
                } else {
                    sum += 28;
                }
            } else {
                sum += 31;
            }
        }
        sum += day;
        return sum;
    }
}



(DFS)

JAVA 代码

//DFS + 动态优化
import java.io.*;

public class Main{
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int a[][] = new int[6][6],sX,sY,eX,eY,min = (int)1e8,dx[],dy[];
    static boolean mark[][] = new boolean[6][6];
    public static void main(String[] args) throws IOException{
        //分量
        dx = new int[]{0,0,-1,1};//上下左右
        dy = new int[]{-1,1,0,0};
        for(int x = 0; x < 6; x ++){
            for(int y = 0; y < 6; y ++){
                a[x][y] = next();
            }
        }
        sX = next();
        sY = next();
        eX = next();
        eY = next();
        dfs(sX,sY,0,1);
        System.out.println(min);
    }
    static void dfs(int x,int y,int dept,int state){
        if(x == eX && y == eY){
            min = Math.min(min,dept);
        }
        //动态优化,如果这步已经大于当前最小值那么就不继续走
        if(dept > min) {
            return;
        }
        for(int i = 0; i < 4; i ++){
            int X = x + dx[i];
            int Y = y + dy[i];
            if(X < 0 || X > 5 || Y < 0 || Y > 5){
                continue;
            }
            if(!mark[X][Y]){
                int addDept = a[X][Y] * state;
                mark[X][Y] = true;
                dfs(X,Y,(dept + addDept),((addDept % 4) + 1));
                mark[X][Y] = false;
            }
        }

    }
    static int next() throws IOException{
        in.nextToken();
        return (int)in.nval;
    }
}


活动打卡代码 AcWing 3480. 棋盘游戏

//DFS + 动态优化
import java.io.*;

public class Main{
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int a[][] = new int[6][6],sX,sY,eX,eY,min = (int)1e8,dx[],dy[];
    static boolean mark[][] = new boolean[6][6];
    public static void main(String[] args) throws IOException{
        //分量
        dx = new int[]{0,0,-1,1};//上下左右
        dy = new int[]{-1,1,0,0};
        for(int x = 0; x < 6; x ++){
            for(int y = 0; y < 6; y ++){
                a[x][y] = next();
            }
        }
        sX = next();
        sY = next();
        eX = next();
        eY = next();
        dfs(sX,sY,0,1);
        System.out.println(min);
    }
    static void dfs(int x,int y,int dept,int state){
        if(x == eX && y == eY){
            min = Math.min(min,dept);
        }
        //动态优化,如果这步已经大于当前最小值那么就不继续走
        if(dept > min) {
            return;
        }
        for(int i = 0; i < 4; i ++){
            int X = x + dx[i];
            int Y = y + dy[i];
            if(X < 0 || X > 5 || Y < 0 || Y > 5){
                continue;
            }
            if(!mark[X][Y]){
                int addDept = a[X][Y] * state;
                mark[X][Y] = true;
                dfs(X,Y,(dept + addDept),((addDept % 4) + 1));
                mark[X][Y] = false;
            }
        }

    }
    static int next() throws IOException{
        in.nextToken();
        return (int)in.nval;
    }
}


活动打卡代码 AcWing 3480. 棋盘游戏

import java.io.*;

public class Main{
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int a[][] = new int[6][6],sX,sY,eX,eY,min = (int)1e8,dx[],dy[];
    static boolean mark[][] = new boolean[6][6];
    public static void main(String[] args) throws IOException{
        //分量
        dx = new int[]{0,0,-1,1};//上下左右
        dy = new int[]{-1,1,0,0};
        for(int x = 0; x < 6; x ++){
            for(int y = 0; y < 6; y ++){
                a[x][y] = next();
            }
        }
        sX = next();
        sY = next();
        eX = next();
        eY = next();
        dfs(sX,sY,0,1);
        System.out.println(min);
    }
    static void dfs(int x,int y,int dept,int state){
        if(x == eX && y == eY){
            min = Math.min(min,dept);
        }
        //动态优化,如果这步已经大于当前最小值那么就不继续走
        if(dept > min) {
            return;
        }
        for(int i = 0; i < 4; i ++){
            int X = x + dx[i];
            int Y = y + dy[i];
            if(X < 0 || X > 5 || Y < 0 || Y > 5){
                continue;
            }
            if(!mark[X][Y]){
                int addDept = a[X][Y] * state;
                mark[X][Y] = true;
                dfs(X,Y,(dept + addDept),((addDept % 4) + 1));
                mark[X][Y] = false;
            }
        }

    }
    static int next() throws IOException{
        in.nextToken();
        return (int)in.nval;
    }
}


活动打卡代码 AcWing 3512. 最短距离总和

//Floyd算法 //逆序加入节点
import java.io.*;

public class Main{
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int n,a[][];
    static long sum;
    public static void main(String[] args) throws IOException{
        n = next();
        a = new int[n][n];
        if(n == 1) {
            System.out.println(0);
            return; //特例
        }
        for(int i = 0; i < n; i ++) {//输入
            for(int v = 0; v < n; v ++) {
                a[i][v] = next();
            }
        }
        int x = n - 1;
        for(int y = n - 1; y > 0; y --, x --) {//迭代以x为起点的矩阵
            update(y);
            //累加到sum中
            for(int b = x; b < n; b ++) {
                for(int c = x; c < n; c ++) {
                    sum += a[b][c];
                }
            }
        }

        System.out.println(sum);
    }
    static void update(int v) {
        for(int x = 0; x < n; x ++) {
            for(int y = 0; y < n; y ++) {
                if(a[x][y] > a[x][v] + a[v][y] ) {
                    a[x][y] = a[x][v] + a[v][y]; //x->v->y 以v为中转节点迭代
                }
            }
        }
    }

    static int next() throws IOException{
        in.nextToken();
        return (int)in.nval;
    }
}


活动打卡代码 AcWing 3475. 简单密码

  //字符串、模拟
import java.io.*;

public class Main{
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    static String start,end;
    static char[] line;
    public static void main(String[] args) throws IOException{
        while(true) {
            start = in.readLine();
            if(start.equals("ENDOFINPUT")) {//程序结束
                return;
            }
            line = in.readLine().toCharArray();
            end = in.readLine();
            for(int i = 0; i < line.length; i ++) {
                if(line[i] >= 65 && line[i] <= 90) {
                    line[i] = (char)((line[i] - 95) % 26 + 90); 
                }
            }
            System.out.println(new String(line));
        }
    }
}



JAVA 代码

import java.util.Scanner;

public class Main{
    static Scanner in = new Scanner(System.in);

    static int n,a;
    public static void main(String[] args) {
        n = in.nextInt();
        while(n --> 0) {
            a = in.nextInt();//贪心,全是鸡 or 鸡 + 1是最小值的优解,全是兔是最大值优解
            System.out.println(a % 2 == 1 ? "0 0" : a % 4 == 0 ? a / 4 + " " + a / 2 : a / 4 + 1 + " " + a / 2);
        }
    }
}


活动打卡代码 AcWing 3473. 鸡兔同笼

import java.util.Scanner;

public class Main{
    static Scanner in = new Scanner(System.in);

    static int n,a;
    public static void main(String[] args) {
        n = in.nextInt();
        while(n --> 0) {
            a = in.nextInt();//贪心,全是鸡是最大值的优解,全是兔 or 兔 - 1是最小值最优解
            System.out.println(a % 2 == 1 ? "0 0" : a % 4 == 0 ? a / 4 + " " + a / 2 : a / 4 + 1 + " " + a / 2);
        }
    }
}


活动打卡代码 AcWing 3451. 字符串排序II

//二维dp,空间换时间
import java.io.*;

public class Main{
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int dp[][],sort[]; //dp的索引0存储数组长度
    static char line[];
    public static void main(String[] args) throws IOException{
        String sr;
        while((sr = in.readLine()) != null) {
        line = sr.toCharArray();
        dp = new int[100][10001];
        sort = new int[line.length + 1];
        for(int i = 0 ; i < sort.length; i ++) {//预处理
            sort[i] = -1;
        }
        for(int i = 0; i < line.length; i ++) {//动态分组
            if((line[i] >= 65 && line[i] <= 90) || (line[i] >= 97 && line[i] <= 122)) {
                int index = (line[i] - 65) % 32; //不区分大小写
                dp[index][dp[index][0] + 1] = line[i];
                dp[index][0] ++;
            } else {
                sort[i] = line[i];
            }
        }
        //将字符重新排列到sort中
        int index = 0;
        for(int i = 0; i < dp.length; i ++) {
            for(int o = 1; o < dp[i].length; o ++) {
                while(sort[index] != -1) {//指针移动到需要插入的位置
                    index ++;
                }
                if(o > dp[i][0]) {
                    break;
                }
                sort[index] = (char)dp[i][o];   
            }
        }
        for(int i = 0; i < sort.length - 1; i ++) {
            out.print((char)sort[i]);
        }
        out.println();
        out.flush();
    }
    }
}


活动打卡代码 AcWing 3451. 字符串排序II

//二维数组的排序
//二维dp,空间换时间
import java.io.*;

public class Main{
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int dp[][],sort[]; //dp的索引0存储数组长度
    static char line[];
    public static void main(String[] args) throws IOException{
        String sr = in.readLine();
        while(sr != null) {
        line = sr.toCharArray();
        dp = new int[100][10001];
        sort = new int[line.length + 1];
        for(int i = 0 ; i < sort.length; i ++) {//预处理
            sort[i] = -1;
        }
        for(int i = 0; i < line.length; i ++) {//动态分组
            if(line[i] >= 65 && line[i] <= 122) {
                int index = (line[i] - 65) % 32; //不区分大小写
                dp[index][dp[index][0] + 1] = line[i];
                dp[index][0] ++;
            } else {
                sort[i] = line[i];
            }
        }
        //将字符重新排列到sort中
        int index = 0;
        for(int i = 0; i < dp.length; i ++) {
            for(int o = 1; o < dp[i].length; o ++) {
                while(sort[index] != -1) {//指针移动到需要插入的位置
                    index ++;
                }
                if(o > dp[i][0]) {
                    break;
                }
                sort[index] = (char)dp[i][o];   
            }
        }
        for(int i = 0; i < sort.length - 1; i ++) {
            out.print((char)sort[i]);
        }
        out.println();
        out.flush();
    }
    }
}