首页 > 试题广场 >

挑7

[编程题]挑7
  • 热度指数:150201 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}你需要统计 1n 之间与 7 有关的数字的个数。

\hspace{15pt}7 有关的数字包括:
\hspace{23pt}\bullet\,7 的倍数(如 7, 14, 21 等);
\hspace{23pt}\bullet\,包含数字 7(如 17, 27, 37, \cdots, 70, 71, 72, \cdots 等)。

输入描述:
\hspace{15pt}输入一个整数 n \left(1 \leqq n \leqq 3 \times 10^4\right)


输出描述:
\hspace{15pt}输出一个整数,代表区间内与 7 有关的数字的个数。
示例1

输入

20

输出

3

说明

\hspace{15pt}在这个样例中,与 7 有关的数字有 7, 14, 17
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int count = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 7 == 0) {
                count++;
                continue;
            }
            int temp = i;
            boolean flag = false;
            while (temp > 0) {
                if (temp % 10 == 7) {
                    flag = true;
                    break;
                }
                temp /= 10;
            }
            if (flag) {
                count++;
            }
        }
        System.out.println(count);
    }
}
发表于 2024-09-29 15:52:54 回复(0)
两种思路:
① 逐渐递减, 检查是否有7相关.
② 如果存在某位数与7相关,直接处理该位数. 比如1076. 会直接count +7 , 下一个改处理的数据为1069
讲道理, 第二种方案会更优, 因为会跳过一部分数据.
但实际效果耗时①优于②? 
各位大佬帮忙分析下哈
private static int getCount (int target) {
    int count = 0;
    while (target > 6) {
        if (target % 7 == 0) {
            count++;
            target--;
            continue;
        }
        int temp = target;
        while(temp > 0) {
            if (temp % 10 == 7) {
                count++;

                break;
            }
            temp /= 10;
        }
        target--;
    }
    return count;
}



private static int getCount (int target) {
    int count = 0;
    while (target > 6) {
        if (target % 7 == 0) {
            count++;
            target--;
            continue;
        }
        int temp = target;
        int R = 1;
        boolean flag = false;
        while(temp > 0) {
            if (temp % 10 == 7) {
                flag = true;
                break;
            }
            R *= 10;
            temp /= 10;
        }

        if (flag) {
            R = (target - (target / R) * R) + 1;
            target -= R;
            count += R;
        } else {
            target--;
        }
    }
    return count;
}


编辑于 2024-04-08 21:25:48 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        for(int i = 1; i <= n; i++){
            if( i % 7 == 0 || (i+"").contains("7")) 
                count++;
        }
        System.out.println(count);
    }
}

发表于 2023-11-25 15:51:42 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();

        int n = Integer.parseInt(line);

        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (String.valueOf(i).contains("7") || i % 7 == 0) {
                cnt++;
            }
        }

        System.out.println(cnt);
    }
}

发表于 2023-08-10 15:16:21 回复(1)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int num=0;
        for(int i=1;i<=n;i++){
            if(i%7==0) num++;
            if((i%7!=0)&&(String.valueOf(i).contains("7"))){
                num++;
            }
        }
        System.out.println(num);
    }
}
发表于 2023-07-15 18:16:44 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int sum = 0;
            for (int i = 1; i <= a; i++) {
                if(String.valueOf(i).contains("7")){
                    sum++;
                }else {
                    if(i%7==0){
                        sum++;
                    }
                }
            }
            System.out.println(sum);
        }
    }
}


发表于 2023-06-10 00:14:30 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int sum=0;
        int num=in.nextInt();
        for(int i=1;i<=num;i++){
            if(getResult(i)==true){
                sum++;
            }
        }
        System.out.print(sum);
    }
    public static boolean getResult(int num){
        if(num%7==0){
            return true;
        }else{
            String str=num+"";
            for (char c : str.toCharArray()) {
                if(c=='7'){
                    return true;
                }
            }
        }
        return false;
    }

}

发表于 2023-06-02 16:50:42 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        int count = 0;
        for(int i = 1;i <= a;i++){
            if(i % 10 == 7 || i % 7 ==0 || String.valueOf(i).contains("7")){
                count++;
            }
        }
        System.out.println(count);
    }
}

发表于 2023-06-01 14:05:35 回复(0)
华为OD深圳/西安岗位,部门急招,薪资20-80w. 部门有专门机考辅导人员,每周开视频讲座。机考分数要求为全公司最低,机考通过2天内可结束所有面试。欢迎叨扰:***********
编辑于 2023-05-10 16:00:13 回复(0)
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        int count = 0 ;
        for (int i = 1; i <= input; i++) {
            if (i%7==0 || i%10==7 || i/10%10==7 || i/100%10==7 || i/1000%10==7)
                count++;
        }
        System.out.println(count);
    }
发表于 2023-05-06 10:15:03 回复(0)
我这逻辑比最热题解复杂,列举各种情况,结果耗时比最热题解耗时还长!
看来递归是真的耗时,也许方法层级太多真的会拖慢运行速度吧。

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            HashSet<Integer> set = new HashSet<>();
            int m= 7;
            while (m<=n){
                set.add(m);
                m += 7;
            }
            m =n;
            int count =0;
            while (m>0){
                count++;
                m /= 10;
            }
            for (int i = 0; i < count; i++) {
                test(n, count, i, "", 0, set);
            }
            System.out.println(set.size());
        }
    }

    private static void test(int n, int count, int k, String str, int m, HashSet<Integer> set) {
        if (m==1 && n/Math.pow(10,count-1)<Integer.parseInt(str)){
            return;
        }
        if (m>count-1){
            int num = Integer.parseInt(str);
            if (num<=n){
                set.add(num);
            }
            return;
        }
        if (k == m){
            test(n, count, k, str+"7", m+1, set);
        }else {
            test(n, count, k, str+"0", m+1, set);
            test(n, count, k, str+"1", m+1, set);
            test(n, count, k, str+"2", m+1, set);
            test(n, count, k, str+"3", m+1, set);
            test(n, count, k, str+"4", m+1, set);
            test(n, count, k, str+"5", m+1, set);
            test(n, count, k, str+"6", m+1, set);
            test(n, count, k, str+"7", m+1, set);
            test(n, count, k, str+"8", m+1, set);
            test(n, count, k, str+"9", m+1, set);
        }
    }

发表于 2023-04-11 21:49:13 回复(0)
尽可能少遍历一些数字(
利用位权重跳过部分数据遍历
import java.util.*;
//利用位权重跳过部分数据遍历
//如遍历到1700时,直接跳到1800,因为17xx均为目标数
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n =in.nextInt();
            int k = 7;
            int count = 0;
            out: for (int i = k; i <= n;i++) {
                if(i%k==0){
                    count++;
                    continue ;
                }
                int index = 1;//从个位开始寻找是否有7,index为每一位的权重
                while(index<i){
                    if(i/(index)%10==k){
                        int limit = Math.min((i/index+1)*index-1,n);//当前权重上限不能比n大!!!!!
                        count+=limit-i+1;//直接记录i与当前权重上限中有多少个数
                        i=limit;//直接跳到权重上限
                        continue out;
                    }
                    index*=10;//权重增加,代表将要判定左边一位的数字
                }
            }
            System.out.println(count);
        }
    }
}


发表于 2023-03-20 18:37:49 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int count = 0;
            for(int i=1; i<=a;i++){
                if(i%7==0){
                
                 count++;   
                 continue;
                }
                int x = i;
                int m = 0;
                while(x>0){
                    if(x%10==7){
                       
                        count++;
                        break;
                    }
                    x=x/10;
                }
                
                
            }
            System.out.println(count);
        }
    }
}

发表于 2023-03-05 14:42:05 回复(0)
我这个数位DP为什么是错的?
import java.util.Scanner; 
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        //7的倍数or 含有7,前者好计算,除法就行
        //后者使用记忆化搜索,主动构造出所有可能的数字
        Main m = new Main(n);
        //别忘了加上7的倍数,但是7的倍数里也要有包含7的需要排除
        int numHava7 = m.memoryDfs(0, true);
        //计算是7的倍数,但是不包含数字7的个数
        int numNo7 = 0;
        for (int x = 7; x <= n; x += 7) {
            if (!String.valueOf(x).contains("7")) {
                numNo7 += 1;
            }
        }

        System.out.print(numHava7 + numNo7);
    }

    public Main(int n) {
        this.maxStr = String.valueOf(n);
        this.numOf7 = new int[maxStr.length()];
        Arrays.fill(numOf7, -1);
        this.powOf10 = new int[maxStr.length()];
        powOf10[0] = 1;
        for (int i = 1; i < powOf10.length; ++i) {
            powOf10[i] = 10 * powOf10[i-1];
        }
    }

    //需要记忆化的是:没有受到isLimit最大值限制的后续低位包含7的可能个数,
    //如果当前构造的数字已经包含7了,则只需要加上全部后续的数字的个数
    //如果受到isLimit的最大值限制,则需要计算,但不需要记忆,因为这种情况只会出现一次
    public int[] numOf7;
    private int[] powOf10;
    private String maxStr;
    private int memoryDfs(int index, boolean isLimit) {
        if (index == maxStr.length()) {
            //来到结尾,
            return 0;
        }
        //求 index .. n 存在多少个包含7的数字
        if (isLimit || numOf7[index] == -1) {
            //如果受到最大数限制&nbs***bsp;没有记忆,则计算
            int maxCurNum = isLimit ? (maxStr.charAt(index) - '0') : 9;
            int sumNum = 0;
            for (int cur = 0; cur <= maxCurNum; ++cur) {
                //如果当前数字就是7
                if (cur == 7) {
                    //如果受到最大值限制,则数量=剩余位的最大值 + 1(包括000)
                    //如果不受到最大值限制,则数量=10^剩余位
                    String lowMaxStr = maxStr.substring(index+1);
                    int lowMaxInt = "".equals(lowMaxStr) ? 0 : Integer.parseInt(lowMaxStr);
                    sumNum += (isLimit ? (lowMaxInt + 1) : powOf10[maxStr.length()-index-1]);
                } else {
                    sumNum += memoryDfs(index + 1, isLimit && (cur == maxCurNum));
                }
            }

            if (!isLimit) {
                //注意,只记忆不受到最大值限制的结果
                numOf7[index] = sumNum;
            } else {
                //如果受到最大值限制,直接返回结果了
                return sumNum;
            }
        }

        return numOf7[index];
    }
}








发表于 2023-03-04 11:32:17 回复(0)
运行时间:19ms,占用内存:9628KB
Java实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int ans = 0; // 答案
        // i从7遍历到n,判断i是否能被7整除,即i % 7 == 0,是则ans+1,否则再判断i是否包含7,是则ans+1
        for (int i = 7; i <= n; i++) { // 跳过1-6,与7无关
            if (i % 7  == 0) {
                ans++;
            } else {
                // 使用整数的计算,比使用String.contains快
                // 通过将cur不断除以10再求余,可获得cur每一位的数
                // 如:171,171/10 == 17, 17 % 10 == 7
                int cur = i;
                while (cur > 0) {
                    int mod = cur % 10;
                    if (mod == 7) {
                        ans++;
                        break;
                    }
                    cur /= 10;
                }
            }
        }
        System.out.println(ans);
    }
}


发表于 2022-08-13 18:33:59 回复(0)
实测操作整数比操作字符串快很多
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(br.readLine());
        int arr[] = {10, 100, 1000};
        get7(num, arr);
//         get8(num);
    }

    public static void get7(int num, int[] arr){
        int count = 0;
        for (int i = 1; i <= num; ++i){
            if (i % 7 == 0){
                count++;
                continue;
            }
            for (int j = 0; j < arr.length; ++j){
                if (i % arr[0] == 7){
                    count ++;
                    break;
                }
                if ((i / arr[j]) % arr[0] == 7){
                    count++;
                    break;
                }
            }
        }
        System.out.println(count);
    }
    
    public static void get8(int num){
        int count = 0;
        String str;
        for (int i = 1; i <= num; ++i){
            str = String.valueOf(i);
            if (str.contains("7") || i % 7 == 0){
                count++;
            }
        }
        System.out.println(count);
    }
}


发表于 2022-08-04 17:14:11 回复(0)
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int n = Integer.parseInt(str);
        int count = 0;
        for(int i = 1; i <= n; i++){
            if(String.valueOf(i).contains("7")||i%7==0){
                count++;
            }
        }
        System.out.println(count);
    }
}

发表于 2022-07-20 16:00:19 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int num = sc.nextInt();
            int count = 0;
            for (int i = 1; i <= num; i++) {
                if (i % 7 == 0 || (i + "").contains("7")) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
发表于 2022-07-10 16:46:34 回复(0)
//一直在想能不能不遍历或者优化遍历跨度 实在不好想想抄答案 结果大家都是在遍历 那我放心留
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (String.valueOf(i).contains("7") || i % 7 == 0) {
                res++;
            }
        }
        System.out.println(res);
    }
}

发表于 2022-07-02 20:47:03 回复(0)

问题信息

难度:
56条回答 25117浏览

热门推荐

通过挑战的用户

查看代码
挑7