ccfcsp认证历年考试 java 解题随手记(1,2题)持续更新

博主分享了备考CCFCSP时的Java解题经验,涵盖多个年份的题目,包括线性分类器、稀疏向量、数组操作等。遇到的挑战包括内存限制、时间效率及特定数据结构的使用。笔记中提到部分题目存在运行错误,期待读者指正。
摘要由CSDN通过智能技术生成

 小声碎碎念:这些都是我在备考ccf时写的代码,大部分代码都是可以得满分,有几个题我也不会改,希望有大佬看到错误能帮我指出来

201312-1 出现次数最多的数

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        HashMap<Integer,Integer> mp = new HashMap<>();
        int n = sc.nextInt();
        int[] arr = new int[]{0,0};
        for (int i = 0; i < n; i++) {
            int p = sc.nextInt();
            mp.put(p,mp.getOrDefault(p,0)+1);
        }
        for(Integer key:mp.keySet()){
            if(mp.get(key)>arr[1]){
                arr[1] = mp.get(key);
                arr[0] = key;
            }else if(mp.get(key)==arr[1] &&key<arr[0]){
                arr[0] = key;
            }
        }
    System.out.println(arr[0]);
    }
}

201312-2  ISBN号码  

import java.util.*;

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

        String[] st = sc.next().split("-");
        String ss = st[0]+st[1]+st[2];
        int count = 0;
        for(int i =0;i<ss.length();i++){
            char c =ss.charAt(i);
            count+= (c-'0')*(i+1);

        }
        count = count%11;
        if(count==10 ){
            if( st[3].equals("X")){
                System.out.println("Right");
            }else{
                System.out.println(st[0]+"-"+st[1]+"-"+st[2]+"-X");
            }

        }else  if (Integer.toString(count).equals(st[3])){

            System.out.println("Right");
        }else {
            System.out.println(st[0]+"-"+st[1]+"-"+st[2]+"-"+count);
        }
    }
}

 201403-1  相反数

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count=0;
        HashSet<Integer> hs = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int a =  Math.abs(sc.nextInt());
            if(hs.contains(a)){
                count++;
            }else {
                hs.add(a);
            }
        }
        System.out.println(count);
    }
}

201403-2  窗口

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<int[]>  windows  = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int[] window = new int[5];
            window[0] = i+1;
            for (int j = 1; j < 5; j++) {
                window[j] = sc.nextInt();
            }
            windows.add(window);
        }
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            boolean b = true;
            for (int j = n-1; j >=0 ; j--) {
                if(is_in(x,y,windows.get(j))) {
                    System.out.println(windows.get(j)[0]);
                    int[] w = windows.remove(j);
                    windows.add(w);
                    b =false;
                    break;
                }
            }
            if (b){
                System.out.println("IGNORED");
            }
        }
    }
    static boolean is_in(int x, int y,int[] window){
        int x1 = window[1];
        int y1 = window[2];
        int x2 = window[3];
        int y2 = window[4];
        return  x1<=x && x<=x2  && y1<=y && y<=y2;
    }
}

201409-1  相邻数对

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //系统自带小根堆,可以直接将输入的数据进行排序
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i = 0;i<n;i++){
            heap.add(sc.nextInt());
        }
        int count = 0;
        int h0 = heap.poll();
        while(!heap.isEmpty()){
            int h1 = heap.poll();
            int poor = h1-h0;
            if(poor==1){
                count++;
            }
            h0=h1;
        }
        System.out.println(count);
    }
}

201409-2  画图

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashSet<String> hset = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int x1 = sc.nextInt();
            int y1 
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值