枚举、模拟与排序

本文介绍了AcWing题库中涉及的五道编程题目,包括区间数的枚举、递增三元组、特殊数求和、错误票据处理、回文日期判断、归并排序、移动距离计算、日期问题以及逆序对数量计算,展示了Java中的基本算法应用。
摘要由CSDN通过智能技术生成

1210. 连号区间数 - AcWing题库

import java.util.*;

public class Main{
    static int N = 10010, INF = 0x3f3f3f3f, n;
    static int[] a = new int[N];
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            a[i] = sc.nextInt();
        }
        
        int res = 0;
        for(int i = 0; i < n; i ++){//枚举左端点
            int Max = -INF;
            int Min = INF;
            for(int j = i; j < n; j ++){//枚举右端点
                Max = Math.max(Max, a[j]);
                Min = Math.min(Min, a[j]);
                if(Max - Min == j - i) res ++;
            }
        }
        
        System.out.print(res);
    }
}

1236. 递增三元组 - AcWing题库

 

1245. 特别数的和 - AcWing题库 

import java.util.*;

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

1204. 错误票据 - AcWing题库

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

public class Main{
    static int N = 1000010;
    static boolean[] st = new boolean[N];
    static int u, v;
    
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        while(n -- > 0){
            String[] str = br.readLine().split(" ");
            for(int i = 0; i < str.length; i ++){
                int x = Integer.parseInt(str[i]);
                if(st[x] == true) v = x;
                st[x] = true;
            }
        }
        
        int j = 0;
        while(!st[j]) j ++;
        for(int i = j; i <= N; i ++){
            if(!st[i]){
                u = i;
                break;
            }
        }
        
        System.out.print(u + " " + v);
    }
}

466. 回文日期 - AcWing题库

import java.util.*;

public class Main{
    static int N = 10000;
    static int[] a = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    public static boolean check(int year, int month, int day){
        if(month > 12 || day < 0 || month < 0) return false;
        
        if(month != 2){
            if(day > a[month]) return false;
        }
        
        if(month == 2){
            if(year % 100 != 0 && year % 4 == 0 || year % 400 == 0) return day <= a[month] + 1;
            else return day <= a[month];
        }
        
        return true;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int date1 = sc.nextInt();
        int date2 = sc.nextInt();
        int res = 0;
        
        for(int i = 1; i < N; i ++){
            int x = i;
            int t = i;
            for(int j = 0; j < 4; j ++){
                x = x * 10 + t % 10;
                t = t / 10;
            }
            
            int year = x / 10000;
            int month = x % 10000 / 100;
            int day = x % 100;
            
            if(x >= date1 && x <= date2 && check(year, month, day)) res ++;
        }
        
        System.out.print(res);
    }
}

787. 归并排序 - AcWing题库

import java.util.*;

public class Main{
    static int N = 100010;
    static int[] a = new int[N];
    
    public static void mergeSort(int l, int r){
        if(l >= r) return;
        
        int mid = l + r >> 1;
        mergeSort(l, mid);
        mergeSort(mid + 1, r);
        
        int[] ans = new int[r - l + 1];
        int k = 0, i = l, j = mid + 1;
        while(i <= mid && j <= r){
            if(a[i] < a[j]) ans[k ++] = a[i ++];
            else ans[k ++] = a[j ++];
        }
        while(i <= mid) ans[k ++] = a[i ++];
        while(j <= r) ans[k ++] = a[j ++];
        
        for(j = 0, i = l; i <= r; i ++, j ++){
            a[i] = ans[j];
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            a[i] = sc.nextInt();
        }
        mergeSort(0, n - 1);
        for(int i = 0; i < n; i ++){
            System.out.print(a[i] + " ");
        }
    }
}

 

1219. 移动距离 - AcWing题库

import java.util.*;

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

1229. 日期问题 - AcWing题库

import java.util.*;

public class Main{
    static int[] a = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    public static boolean check(int year, int month, int day){
        if(month <= 0 || month > 12 || day <= 0) return false;
        
        if(month != 2){
            if(day > a[month]) return false;
        }
        
        if(month == 2){
            if(year % 100 != 0 && year % 4 == 0 || year % 400 == 0) return day <= a[month] + 1;
            else return day <= a[month];
        }
        return true;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] s = sc.next().split("/");
        int a = Integer.parseInt(s[0]);
        int b = Integer.parseInt(s[1]);
        int c = Integer.parseInt(s[2]);
        
        for(int i = 19600101; i <= 20591231; i ++){
            int year = i / 10000;
            int month = i % 10000 / 100;
            int day = i % 100;
            
            if(check(year, month, day)){
                if(((year % 100 == a) && month == b && day == c) || ((year % 100 == c) && month == a && day == b) || ((year % 100 == c) && month == b && day == a)){
                    System.out.printf("%d-%02d-%02d\n", year, month, day);
                } 
            }
        }
    }
}

788. 逆序对的数量 - AcWing题库

import java.util.*;

public class Main{
    static int N = 100010;
    static int[] a = new int[N];
    static long buff;
    
    public static long mergeSort(int l, int r){
        if(l >= r) return 0;
        
        int mid = l + r >> 1;
        buff = mergeSort(l, mid) + mergeSort(mid + 1, r);
        

        int[] res = new int[r - l + 1];
        int k = 0, i = l, j = mid + 1;
        while(i <= mid && j <= r){
            if(a[i] <= a[j]) res[k ++] = a[i ++];//这里必须是小于等于
            else{
                res[k ++] = a[j ++];
                buff += mid - i + 1;
            }
        }
        
        while(i <= mid) res[k ++] = a[i ++];
        while(j <= r) res[k ++] = a[j ++];
        
        for(j = 0, i = l; i <= r; i ++, j ++){
            a[i] = res[j];
        }
        
        return buff;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            a[i] = sc.nextInt();
        }
        System.out.print(mergeSort(0, n - 1));
    }
}

1241. 外卖店优先级 - AcWing题库

 

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值