题解

挑7有两种情况:

  • i%7==0 表示 i是7的倍数。
  • String.valueOf(i).indexOf('7') != -1 说明字符串中包含7

代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int count = 0;
            for (int i = 1; i <= n; i++) {
                if (i % 7 == 0 || String.valueOf(i).indexOf('7') != -1) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}