CCF计算机软件能力认证201912前两题java满分解

文章讲述了两个Java编程题目,涉及输入整数数组的处理,包括查找含有7的数字的子数组和计算具有特定关系的整数对计数。主要展示了如何使用Scanner和BufferedReader进行用户输入,以及如何遍历数组执行算法计算。
摘要由CSDN通过智能技术生成

第一题

package ccf201912;

import java.util.Scanner;

public class p1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] p = new int[4];
        int count = 1;
        int num = 1;
        while (count < n) {
            for (int i = 0; i < 4; i++) {

                if (hasSeven(num)) {
                    p[i]++;
                    count--;
                }
                if (count >= n) break;
                num++;
                count++;
            }
        }
        for (int i = 0; i < 4; i++) {
            System.out.println(p[i]);
        }
    }

    public static boolean hasSeven(int num) {
        if (num % 7 == 0) {
            return true;
        }
        String str = "" + num;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '7') {
                return true;
            }
        }
        return false;
    }
}

第二题

package ccf201912;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p2 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] temp = bf.readLine().split(" ");
        int n = Integer.parseInt(temp[0]);
        long[] x = new long[n + 1];
        long[] y = new long[n + 1];
        long[] count = new long[n + 1];
        int[] res = new int[5];
        for (int i = 1; i <= n; i++) {
            String[] line = bf.readLine().split(" ");
            x[i] = Integer.parseInt(line[0]);
            y[i] = Integer.parseInt(line[1]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (x[j] == x[i] && y[j] == y[i] - 1) {
                    count[i]++;
                    count[j]++;
                }
                if (x[j] == x[i] && y[j] == y[i] + 1) {
                    count[i]++;
                    count[j]++;
                }
                if (x[j] == x[i] - 1 && y[j] == y[i]) {
                    count[i]++;
                    count[j]++;
                }
                if (x[j] == x[i] + 1 && y[j] == y[i]) {
                    count[i]++;
                    count[j]++;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (count[i] >= 4) {
                for (int j = 1; j <= n; j++) {
                    if (x[j] == x[i] - 1 && y[j] == y[i] - 1) {
                        count[i]++;
                    }
                    if (x[j] == x[i] - 1 && y[j] == y[i] + 1) {
                        count[i]++;
                    }
                    if (x[j] == x[i] + 1 && y[j] == y[i] - 1) {
                        count[i]++;
                    }
                    if (x[j] == x[i] + 1 && y[j] == y[i] + 1) {
                        count[i]++;
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (count[i] >= 4) {
                if (count[i] - 4 == 0) {
                    res[0]++;
                } else if (count[i] - 4 == 1) {
                    res[1]++;
                } else if (count[i] - 4 == 2) {
                    res[2]++;
                } else if (count[i] - 4 == 3) {
                    res[3]++;
                } else if (count[i] - 4 == 4) {
                    res[4]++;
                }
            }
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(res[i]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值