小声碎碎念:这些都是我在备考ccf时写的代码,大部分代码都是可以得满分,有几个题我也不会改,希望有大佬看到错误能帮我指出来
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
HashMap 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
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
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 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);
}
}
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 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;
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//系统自带小根堆,可以直接将输入的数据进行排序
PriorityQueue heap = new PriorityQueue<>();
for (int i = 0;i
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashSet hset = new HashSet<>();
for (int i = 0; i < n; i++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
for (int j = x1; j
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap hmap = new HashMap<>();
for (int i = 0; i < n; i++) {
int r = sc.nextInt();
hmap.put(r,hmap.getOrDefault(r,0)+1);
System.out.print(hmap.get(r)+" ");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] jz =new int[n][n];
for (int i = 0; i =0&&y=0&& y=0&& y=0){
System.out.print(jz[x][y]+" ");
x++;
y--;
}
x--;
y++;
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
String str = sc.readLine();
String[] vars = str.trim().split(" ");
int n = Integer.parseInt(vars[0]);
int m = Integer.parseInt(vars[1]);
int[][] jz = new int[n][m];
for (int i = 0; i < n; i++) {
str = sc.readLine();
vars = str.trim().split(" ");
for(int j = 0; j < m; j++) {
jz[i][j] = Integer.parseInt(vars[j]);
}
}
for (int i = m-1; i >=0 ; i--) {
for (int j = 0; j < n; j++) {
System.out.print( jz[j][i]+" ");
}
System.out.println();
}
}
}
如果用Scanner会超内存,只能得90分,要用BufferedReader。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map map = new HashMap();
for(int i = 0;i < n; i++) {
int num = sc.nextInt();
if(map.containsKey(num)) {
map.put(num, map.get(num)+1);
}else {
map.put(num, 1);
}
}
// map按照Value从大到小,若Value相同,再按照Key从小到大
ArrayList> arrayList = new
ArrayList>(map.entrySet());
Collections.sort(arrayList,new Comparator>(){
public int compare(Entry o1, Entry o2) {
int result = o2.getValue() -o1.getValue();
if(result != 0) {
return result;
}else {
return o1.getKey()-o2.getKey();
}
}
});
//遍历list得到map里面排序后的元素
for(Entry en: arrayList) {
System.out.println(en.getKey()+" "+ en.getValue());
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
nums[0] = sc.nextInt();
int count = 1;
for (int i = 1; i < nums.length; i++) {
nums[i] = sc.nextInt();
if(nums[i]!=nums[i-1]) {
count++;
}
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
int d = sc.nextInt();
int month = 1,date = 0;
int[] r = {31,28,31,30,31,30,31,31,30,31,30,31};
//闰年
if (y%400==0 ||(y%4==0 && y%100!=0)) {
r[1]+=1;
for (int i = 0; i < r.length; i++) {
if(d-r[i]>0) {
d = d-r[i];
month++;
}else{
date = d;
break;
}
}
}else {
for (int i = 0; i < r.length; i++) {
if(d-r[i]>0) {
d = d-r[i];
month++;
}else{
date = d;
break;
}
}
}
System.out.println(month);
System.out.println(date);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.next();
int count = 0;
for (int i = 0; i < n.length(); i++) {
count=count+(n.charAt(i)-'0');
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
//定义两个数组,在另外一个数组上进项标记
int[][] gz = new int[n][m];
int[][] flag = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
gz[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//先标出一行中相同的
if(j+1
感觉前面的题和近几年的题难易程度稍有变化,中间就先不写了,之后有空再补吧
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] arr = new int[n][3];
for (int i = 0; i < n; i++) {
arr[i][0] = scanner.nextInt();
arr[i][1] = scanner.nextInt();
String s = scanner.next();
if(s.equals("A")) {
arr[i][2] = 0;
}else {
arr[i][2] = 1;
}
}
for (int i = 0; i < m; i++) {
int st0 = scanner.nextInt();
int st1 = scanner.nextInt();
int st2 = scanner.nextInt();
HashSet zuoset = new HashSet();
HashSet youset = new HashSet();
for (int j = 0; j < arr.length; j++) {
int q = st0+st1*arr[j][0]+st2*arr[j][1];
if (q>0) {
zuoset.add(arr[j][2]);
} else if(q<0) {
youset.add(arr[j][2]);
}
}
if (zuoset.size()==1&&youset.size()==1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
String str = sc.readLine();
String[] vars = str.trim().split(" ");
int n = Integer.parseInt(vars[0]);
int a = Integer.parseInt(vars[1]);
int b = Integer.parseInt(vars[2]);
Map uMap = new HashMap();
long s = 0l;
for (int i = 0; i < a; i++) {
str = sc.readLine();
vars = str.trim().split(" ");
int k1 = Integer.parseInt(vars[0]);
int v2 = Integer.parseInt(vars[1]);
uMap.put(k1, v2);
}
for (int i = 0; i < b; i++) {
str = sc.readLine();
vars = str.trim().split(" ");
k = Integer.parseInt(vars[0]);
v = Integer.parseInt(vars[1]);
if (uMap.containsKey(k)) {
s+=uMap.get(k)*v;
}
}
System.out.println(s);
}
}
注意:这道题用Scanner会内存超限,必须用BuffredReader。用数组会时间超时,所以要用HashMap。count用int也会报错,要用long。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int x0 = scanner.nextInt();
int y0 = scanner.nextInt();
int[][] l = new int[n][2];
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
l[i][0]=i+1;
l[i][1]=(x-x0)*(x-x0)+(y-y0)*(y-y0);
}
Arrays.sort(l,(o1,o2)->(o1[1]-o2[1]==0?o1[0]-o2[0]:o1[1]-o2[1]));
for (int i = 0; i < 3; i++) {
System.out.println(l[i][0]);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
int t = scanner.nextInt();
int xl = scanner.nextInt();
int yd = scanner.nextInt();
int xr = scanner.nextInt();
int yu = scanner.nextInt();
int jgnum = 0;
int dlnum = 0;
for (int i = 0; i < n; i++) {
boolean dltemp = false;
boolean jgtemp = false;
int temp = 0;
for (int j = 0; j < 2*t; j=j+2){
int x = scanner.nextInt();
int y = scanner.nextInt();
if(x<=xr&&x>=xl&&y<=yu&&y>=yd){
temp++;
jgtemp = true;
}else {
temp=0;
}
if(temp>=k) {
dltemp=true;
}
}
if(jgtemp) {
jgnum++;
}
if (dltemp) {
dlnum++;
}
}
System.out.println(jgnum);
System.out.println(dlnum);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
int w = scanner.nextInt();
int score = scanner.nextInt();
count+=w*score;
}
System.out.println(Math.max(0, count));
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] num = new int[n][2];//记录前面的阈值
int[] pre = new int[n+1];//记录当前num位置之前的result总数和,不含当前位置
for (int i = 0; i < n; i++) {
num[i][0] = scanner.nextInt();
num[i][1] = scanner.nextInt();
}
//先按第一列排序,相同的话按第二列排序
Arrays.sort(num,(o1,o2)->o1[0]-o2[0]==0?o1[1]-o2[1]:o1[0]-o2[0]);
for (int i = 1; i <= n; i++) {
pre[i] = pre[i-1]+num[i-1][1];
}
Set set = new HashSet();
int maxMatch = -1; //最大匹配数
int maxSuitable = -1; // 最合适的成绩
for (int i = 0; i < n; i++) {
if(set.contains(num[i][0])) {
continue;
}
set.add(num[i][0]);
int zero = i-pre[i];
int one = pre[n]-pre[i];
int total = zero+one;
if (total>=maxMatch) {
maxSuitable = num[i][0];
maxMatch = total;
}
}
System.out.println(maxSuitable);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int L = scanner.nextInt();
HashMap map = new HashMap();
for (int i = 0; i < n*m; i++) {
int c = scanner.nextInt();
map.put(c, map.getOrDefault(c, 0)+1);
}
for (int i = 0; i < L; i++) {
System.out.print(map.getOrDefault(i, 0)+" ");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int L = scanner.nextInt();
int r = scanner.nextInt();
int t = scanner.nextInt();
int[][] arr = new int[n][n];
int[][] pre = new int[n+1][n+1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}
for (int i = 1; i < n+1; i++) {
for (int j = 1; j < n+1; j++) {
pre[i][j] = pre[i-1][j] + pre[i][j-1] + arr[i-1][j-1] - pre[i-1][j-1];
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int maxi = Math.min(n-1,i+r);
int mini = Math.max(0,i-r);
int maxj = Math.min(n-1, j+r);
int minj = Math.max(0, j-r);
int temp = pre[maxi+1][maxj+1]-pre[maxi+1][minj]-pre[mini][maxj+1]+pre[mini][minj];
if(temp<=(maxi-mini+1)*(maxj-minj+1)*t) {
count++;
}
}
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = -1;
int maxx = 0,minn = 0;
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
maxx += temp;
if(temp>x) {
minn+=temp;
x = temp;
}
}
System.out.println(maxx);
System.out.println(minn);
}
}
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n+2];
TreeSet set = new TreeSet();
TreeMap> map = new TreeMap>();
int pre = 0;
for (int i = 1; i <= n; i++) {
int temp = sc.nextInt();
arr[i] = temp;
if(map.containsKey(arr[i])) {
map.get(arr[i]).add(i);
}else {
ArrayList tmp = new ArrayList();
tmp.add(i);
map.put(arr[i], tmp);
}
if (arr[i]!=0 && arr[i-1]==0) {
pre++;
}
}
set.add(pre);
for (Map.Entry> entry : map.entrySet()) {
Integer key = entry.getKey();
if(key == 0) continue;
ArrayList val = entry.getValue();
for (int i = 0; i < val.size(); i++) {
if(arr[val.get(i)-1]!=0 &&arr[val.get(i)+1]!=0) {
pre++;
}else if(arr[val.get(i)-1]==0 &&arr[val.get(i)+1]==0){
pre--;
}
arr[val.get(i)] = 0;
}
set.add(pre);
}
System.out.println(set.last());
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int N = sc.nextInt();
int[] diff = new int[N+1];
//虽然这个题可以用一些数学计算写出满分,但我个人还是用了差分
//最标准的差分用法
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
diff[temp+1]+=1;
}
int count = 0;
for (int i = 1; i < diff.length; i++) {
diff[i] += diff[i-1];
count+=diff[i];
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int N = sc.nextInt();
int[] diff_fx = new int[N+1];
int[] diff_gx = new int[N+1];
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
diff_fx[temp+1]+=1;
}
int r = N/(n+1);
for (int i = 1; i <= N/r; i++) {
if(i*r+1<=N) {
diff_gx[i*r+1]+=1;
}
}
long count = 0;
for (int i = 1; i < N+1; i++) {
diff_fx[i]+=diff_fx[i-1];
diff_gx[i]+=diff_gx[i-1];
}
for (int i = 1; i < N+1; i++) {
count+=Math.abs(diff_fx[i]-diff_gx[i]);
}
System.out.println(count);
}
}
虽然但是,运行错误,我还不会改,有大佬帮我指一下错误嘛 ε=(´ο`*)))
package ccf;
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 k = sc.nextInt();
int count = 0;
HashSet set = new HashSet();
set.add(0);
for (int i = 0; i < k; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if(set.contains(y)) {
set.add(x);
}else {
count++;
set.add(x);
}
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//出行计划数目
int m = sc.nextInt();//查询个数
int k = sc.nextInt();//结果等待时间
int[] diff_time = new int[500001];
//差分 记录从出行时需要的核酸可以是什么时候出的结果
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if (x-y<=0) {//核酸出结果时间可以在x这个时间之前的任意时间
diff_time[1]+=1;
diff_time[x+1]-=1;
}else {//核酸出结果时间只能在x-y+1到x期间
diff_time[x-y+1]+=1;
diff_time[x+1]-=1;
}
}
//该时间点出的结果可以通行几个计划
for (int i = 1; i < diff_time.length; i++) {
diff_time[i] += diff_time[i-1];
}
while (m>0) {
m--;
int t = sc.nextInt();
System.out.println(diff_time[t+k]);
}
}
}