小声碎碎念:这些都是我在备考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