挑7

输出 1到n之间 的与 7 有关数字的个数。 一个数与7有关是指这个数是 7 的倍数,或者是包含 7 的数字(如 17 ,27 ,37 … 70 ,71 ,72 ,73…) 数据范围:

数据范围: 1≤n≤30000

输入描述:

一个正整数 n 。( n 不大于 30000 )

输出描述:

一个整数,表示1到n之间的与7有关的数字个数。

示例1

输入
20
输出
3
说明
输入20,1到20之间有关的数字包括7,14,17共3个。

Java 编程

package cn.net.javapub.javaintroduction.example;

/**
 * @author: shiyuwang
 */

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = bf.readLine()) != null) {
            int a = Integer.parseInt(str);
            System.out.println(findseven(a));
        }
    }

    public static int findseven(int a) {
        int x = 0;
        for (int i = 1; i <= a; i++) {
            if (i % 7 == 0) {
                x++;
            } else if (i % 10 == 7) {
                x++;
            } else if ((i / 10) % 10 == 7) {
                x++;
            } else if ((i / 100) % 10 == 7) {
                x++;
            } else if ((i / 1000) % 10 == 7) {
                x++;
            }
        }
        return x;
    }
}

展示效果:

华为OD机试 - 挑7 (Java 2024 E卷 100分)_Java