CCF CSP认证JAVA(七)

201912-1报数

在这里插入图片描述

public class demo21 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int[] cnt = new int[4];
        int count =1;
        for( int i=1;count<=m;i++){
            if (!hasSeven(i)){
                count++;
            }else {
                cnt[(i-1)%4]++;
            }
        }
        for(int x:cnt){
            System.out.println(x);
        }
    }
    public static  boolean hasSeven(int m){
        if(m%7==0){
            return true;
        }
        while (m!=0){
            if(m%10==7){
                return true;
            }else {
                m = m/10;
            }
        }
        return false;
    }
}

202006-2稀疏向量

在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
class Input{
    StreamTokenizer streamTokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public int nextInt() throws IOException {
        streamTokenizer.nextToken();
        return (int) streamTokenizer.nval;
    }
}
public class demo22 {
    public static void main(String [] args) throws IOException {
        Input in = new Input();
        int n = in.nextInt(),a = in.nextInt(),b = in.nextInt();
        long sum = 0;
        Map<Integer,Integer> map = new HashMap<>();
        while (a--!=0){
            map.put(in.nextInt(), in.nextInt());
        }
        while (b--!=0){
            sum += (long) map.getOrDefault(in.nextInt(), 0) *in.nextInt();
        }
        System.out.println(sum);
    }
}

Map.getOrDefault()的用法

当Map中有这个key时,就返回这个key对应的value,如果没有就使用默认值defaultValue.

public class map_getOrDefault {
     public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		  map.put("name", "我的");
          map.put("age", "24");
          map.put("sex", "女");
          
          String name = map.getOrDefault("name", "test");
          System.out.println(name);
          
          String address = map.getOrDefault("address", "北京");
          System.out.println(address);
	}
}
//结果
我的
北京

201912-2回收站选址

在这里插入图片描述

Map map=new HashMap()推荐这种写法。这样换了实现类也不会影响到调用map的地方了,因为map是个接口,换了任何具体的实现类都不会改变接口的方法。

HashMap map=new HashMap()。语法上是对的,只有你想要调用HashMap 里面有的方法而Map里面没有的时候这样写。

咋说呢,这位仁兄的方法我只能说是叹为观止:

(1条消息) JAVA CCF-201912-2 回收站选址_不断奔跑-CSDN博客

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

public class Main {
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] score = new int[5];
        int[][] way1 = {{0,1},{0,-1},{-1,0},{1,0}};//上下左右四个方向
        int[][] way2 = {{1,1},{1,-1},{-1,1},{-1,-1}};//四个斜方向对角
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();//行:列集合
        for(int i=0;i<n;i++){
            int row = in.nextInt(),column = in.nextInt();
            if(!map.containsKey(row)){
                map.put(row,new HashSet<>());
            }
            map.get(row).add(column);
        }
        for(int row: map.keySet()){
            for(int column : map.get(row)){
                boolean flag = true;//上下左右是否有垃圾
                for(int[] way: way1){
                    int trow = row + way[0],tcolumn = column + way[1];
                    if (!(map.containsKey(trow)&&map.get(trow).contains(tcolumn))){
                        flag = false;
                        break;
                    }
                }
                if (flag){
                    int count = 0;//斜方向有多少个垃圾
                    for(int[] way:way2){
                        int trow = row + way[0],tcolumn = column + way[1];
                        if (map.containsKey(trow)&&map.get(trow).contains(tcolumn)){
                            count++;
                        }
                    }
                    score[count]++;
                }
            }

        }
        for (int val: score){
            System.out.println(val);
        }
    }
}

201712-1最小差值

问题描述

给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值。

输入格式

输入第一行包含一个整数n
  第二行包含n个正整数,相邻整数之间使用一个空格分隔。

输出格式

输出一个整数,表示答案。

样例输入

5
1 5 4 8 20

样例输出

1

样例说明

相差最小的两个数是5和4,它们之间的差值是1。

样例输入

5
9 3 6 1 3

样例输出

0

样例说明

有两个相同的数3,它们之间的差值是0.

数据规模和约定

对于所有评测用例,2 ≤ n ≤ 1000,每个给定的整数都是不超过10000的正整数。

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            arr[i]=scanner.nextInt();
        }
        scanner.close();
        Arrays.sort(arr);
        int min = arr[1]-arr[0];
        while (n--!=2){
            int x = arr[n-1]-arr[n-2];
            min = Math.min(x,min);
        }
        System.out.println(min);
    }
}


今日推歌

—《他只是经过》

他只是经过 你的 世界
并没有停留
只偶尔听说 谁说
关于他你又会触动
眼泪又滴落 闪动
太多的话都变沉默
你给的问候 温柔
所有都被一笔带过

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星回昭以烂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值