头像

LORE




离线:1天前


最近来访(4)
用户头像
200
用户头像
HollowKnight
用户头像
我要出去乱说
用户头像
knighz

活动打卡代码 AcWing 1219. 移动距离

LORE
1天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    static final int N = 100000010;
    static int[][] arr;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int m = sc.nextInt();
        int n = sc.nextInt();
        m--;n--;
        int x1 = m / w;
        int x2 = n / w;
        int y1 = 0;
        int y2 = 0;
        if(x1 % 2 == 0) y1 = m % w;
        else y1 = w - 1 - (m % w);
        if(x2 % 2 == 0) y2 = n % w;
        else y2 = w - 1 - (n % w);
        System.out.println(Math.abs(x1-x2)+Math.abs(y1-y2));
    }
}


活动打卡代码 AcWing 466. 回文日期

LORE
1天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    //1 3 5 7 8 10 12 (31)
    //4 6 9 11        (30)
    //2               (28)(29)
    // 润年x%4==0&&x%100!=0||x%400==0
    static int[] arr = new int[13];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        arr = a;
        int date1 = sc.nextInt();
        int date2 = sc.nextInt();
        int date,x;
        int res = 0;
        for(int i = 1000;i < 10000;i++)
        {
            date = i;
            x = i;

            while(x>0)
            {
                date = date * 10 + x % 10;
                x = x / 10;
            }

            if(date1 <= date && date <= date2 && isOk(date)) res++;
        }
        System.out.println(res);
    }

    public static boolean isOk(int date)
    {
        int run = 0;
        int year = date / 10000 % 10000;
        int month = date / 100 % 100;
        int day = date % 100;
        if(month > 12 || month == 0 ) return false;

        if(day == 0 || month != 2 && day > arr[month]) return false;

        if(month == 2)
        {
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) run = 1;
            if(day > arr[month]+run) return false;
        }

        return true;
    }
}


活动打卡代码 AcWing 1204. 错误票据

LORE
1天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<Integer>();
        int x = 0;
        String[] s;
        sc.nextLine();
        while(N-->0) 
        {
                s = sc.nextLine().trim().split("\\s+");
                for(int i = 0;i < s.length;i++)
                {
                    list.add(Integer.parseInt(s[i]));
                }
        }
        Collections.sort(list);

        int m = 0;
        int n = 0;
        for(int i = 0; i < list.size()-1;i++)
        {
            if((int)list.get(i+1)==(int)list.get(i)) {
                n = list.get(i);
            }
            else if(list.get(i+1)-list.get(i)==2) m = list.get(i)+1;
        }
        System.out.println(m+" "+n);
    }
}


活动打卡代码 AcWing 1245. 特别数的和

LORE
1天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = 0;
        int n = sc.nextInt();
        for(int i = 1;i <= n;i++)
        {
            int t = i;
            while(t > 0)
            {
                int x = t % 10;
                t = t / 10;
                if(x == 2 || x == 0 || x == 1 || x == 9)
                {
                    s += i;
                    break;
                }
            }
        }
        System.out.println(s);
    }
}


活动打卡代码 AcWing 1236. 递增三元组

LORE
1天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    static final int N = 100010;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[N];
        int[] b = new int[N];
        int[] c = new int[N];

        int[] cnt = new int[N];
        int[] s = new int[N];
        int[] as = new int[N];
        int[] cs = new int[N];
        for(int i = 0;i < n;i++) 
        {
            a[i] = sc.nextInt(); 
            a[i]++;
        }
        for(int i = 0;i < n;i++) 
        {
            b[i] = sc.nextInt(); 
            b[i]++;
        }
        for(int i = 0;i < n;i++) 
        {
            c[i] = sc.nextInt(); 
            c[i]++;
        }

        for(int i = 0;i < n;i++) cnt[a[i]]++;
        for(int i = 1;i < N;i++) s[i] = s[i-1] + cnt[i];
        for(int i = 0;i < n;i++) as[i] = s[b[i]-1];

        cnt = new int[N];
        s = new int[N];

        for(int i = 0;i < n;i++) cnt[c[i]]++;
        for(int i = 1;i < N;i++) s[i] = s[i-1] + cnt[i];
        for(int i = 0;i < n;i++) cs[i] = s[N-1]-s[b[i]];
        long res = 0;
        for(int i = 0;i < n;i++) res += (long)as[i]*cs[i];
        System.out.println(res);
    }
}


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

LORE
2天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n  = sc.nextInt();
        int[] arr = new int[n+1];

        for(int i = 1;i <= n;i++)
            arr[i] = sc.nextInt();

        int ans = 0;
        for(int i = 1;i <= n;i++)
        {
            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
            for(int j = i;j <= n;j++)
            {
                max = Math.max(max,arr[j]);
                min = Math.min(min, arr[j]);
                if(max-min==j-i) ans++;
            }
        }
        System.out.println(ans);
    }
}



LORE
4天前

java语言,利用集合与字符串进行去重

JAVA 代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int n;
    static int m;
    static int[] num;
    static int[] st;
    static ArrayList<String> list;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        list = new ArrayList<String>();
        n = sc.nextInt();
        m = sc.nextInt();
        st = new int[m];
        num = new int[n];

        for(int i = 0;i < n;i++)
            num[i] = sc.nextInt();
        Arrays.sort(num);               //对数组进行排序
        dfs(0,0);

        for(Object k:list)              //输出集合
        {
            System.out.println(k);
        }
    }
    public static void dfs(int c,int index)
    {
        if(c+n-index<m) return;                 //如果当前的已经选了多少个数加上后面可选的数不够选的话就剪枝
        if(c==m)                                //如果已经选满了m个数,就遍历数组,统一加到集合里面
        {
            String s = "";
            for(int i = 0;i < m;i++) {
                if(i<n-1) s += st[i]+" ";       //如果不为最后一个就加上空格
                else s += st[i];
            }
            if(!list.contains(s)) list.add(s);  //如果不重复的的就加到集合里面
            return;
        }

        for(int i = index;i < n;i++)            //从num数组中选
        {
            st[c] = num[i];                     //添加到我们的选中数组中
            dfs(c+1,i+1);
        }
    }
}




LORE
8天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] dp = new int[1001];
        int[] f = new int[1001];
        int n = sc.nextInt();

        for(int i = 1;i <= n;i++)
            dp[i] = sc.nextInt();

        for(int i = 1;i <= n;i++)
        {
            f[i] = 1;
            for(int j = 1;j < i;j++)
            {
                if(dp[i]>dp[j]) f[i] = Math.max(f[i],  f[j]+1);
            }
        }

        int res = 0;

        for(int i = 1;i <= n;i++) res = Math.max(f[i],res);

        System.out.println(res);
    }
}



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

LORE
8天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int[][] dp = new int[110][110];

        int r;
        int c;

        while(T-->0)
        {
            r = sc.nextInt();
            c = sc.nextInt();

            for(int i = 1;i <= r;i++) 
                for(int j = 1;j <= c;j++) 
                    dp[i][j] = sc.nextInt();

            for(int i = 1;i <= r;i++)
                dp[i][1] = dp[i-1][1] + dp[i][1];

            for(int i = 1;i <= c;i++)
                dp[1][i] = dp[1][i-1] + dp[1][i];

            for(int i = 2;i <= r;i++)
            {
                for(int j = 2;j <= c;j++)
                {
                    dp[i][j] += Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
            System.out.println(dp[r][c]);
        }

    }
}



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

LORE
8天前
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] st = new int[1000];

        for(int i = 0;i < n;i++)
        {
            st[i] = sc.nextInt();
        }

        int left = 0;
        int right = 0;

        for(int i = 1;i < n;i++)
        {
            if(Math.abs(st[i]) > Math.abs(st[0]) && st[i] < 0) left++;
            else if(Math.abs(st[i]) < Math.abs(st[0]) && st[i] > 0) right++;
        }

        if((st[0] > 0 && left == 0) || (st[0] < 0 && right == 0) ) System.out.println(1);
        else System.out.println(right+left+1);
    }
}