首页 > 试题广场 >

挑7

[编程题]挑7
  • 热度指数:138253 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输出 1到n之间 的与 7 有关数字的个数。
一个数与7有关是指这个数是 7 的倍数,或者是包含 7 的数字(如 17 ,27 ,37 ... 70 ,71 ,72 ,73...)

数据范围:

输入描述:

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



输出描述:

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

示例1

输入

20

输出

3

说明

输入20,1到20之间有关的数字包括7,14,17共3个。 
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	long num;
	long num_square;

	while(cin >> num)
	{
		int count = 0;
		string num_str;
		for(int i=1;i<=num;++i)
		{
			num_str = to_string(i);
			if (num_str.find('7')!= num_str.npos||i%7==0)
			{
				++count;
			}
		}
		cout << count << endl;
	}
	return 0;
}

发表于 2019-09-16 16:31:28 回复(0)
//提交一个非常传统的想法, 没用大佬们的字符串搜索方式
#include <iostream>

using namespace std;
//确认当前数i每一位是否含有7
int seven(unsigned i)
{
    while (i != 0)
    {
        if (i % 10 == 7)
            return 1;
        i /= 10;
    }
    return 0;
}
/////////////////////////////
int main(void)
{
    int flag1, flag2;
    unsigned i, n, num;
    while (cin >> n)
    {
        num = 0;
        for (i = 1; i <= n; i++)
        {
            if (i % 7 == 0)
            {
                num++;
                continue;
            }
            if ( seven(i) == 1)
            {
                num++;
            }
        }
        cout << num << endl;
    }
    return 0;
}

编辑于 2018-04-24 18:21:49 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n=0;
	while(cin>>n)
	{
	    int num=0;
		for(int i=1;i<=n;i++)
		{
			if(i%7==0)
				num++;
			else
			{
				char c='7';
				string s=to_string(i);
				for(int j=0;j<s.size();j++)
				{
					if(s[j]==c)
					{
						num++;
						break;
					}
				}
			}
		}
		cout<<num<<endl;
	}
	return 0;
}

发表于 2017-08-07 20:29:01 回复(1)
运行时间: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.*;

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.*;
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)
简直不要太强
while True:
    try:
        n = int(input())
        x = []
        for i in range(1,n+1):
            if (i%7 == 0) or ("7" in str(i)):
                x.append(i)
        print(len(x))
    except:
        break

发表于 2022-02-25 14:41:52 回复(1)
nums = set()

i = 7
while i <= 30000:
    nums.add(i)
    i += 7
for i in range(1, 30001):
    if str(i).find("7") != -1:
        nums.add(i)
nums = list(sorted(nums))

try:
    while True:
        n = int(input())
        count = 0
        while nums[count] <= n:
            count += 1
        print(count)
except:
    pass

发表于 2022-01-23 19:54:18 回复(0)
while True:
    try:
        n=int(input())
        res=0
        for i in range (7,n+1):
            if (i%7==0) or "7" in str(i):
                res+=1
        print (res)
    except:
        break

编辑于 2021-07-19 11:32:11 回复(0)
运行时间38ms   占用内存10720KB
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            int cnt = 0;
            for(int i = 1; i <= n; i++){
                if(is7(i)){
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    }
    private static boolean is7(int n){
        if(n % 7 == 0){//7的倍数
            return true;
        }
        while(n > 0){//包含7
            int tmp = n / 10;
            if(n - tmp * 10 == 7){
                return true;
            }
            n /= 10;
        }
        return false;
    }
}



编辑于 2021-07-18 16:18:15 回复(0)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
bool contains7(int x) {
    while(x!=0) {
        if(x%10==7)
            return true;
        x/=10;
    }
    return false;
}
int main()
{
    int n;
    while(cin>>n) {
        int ans=0;
        for(int i=7;i<=n;++i) {
            if(i%7==0 || contains7(i)) {
                ++ans;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

发表于 2021-06-28 11:51:01 回复(0)
import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();
            int result = 0;
            for(int i = 7; i <= n; i++){
                if((String.valueOf(i)).contains("7"))
                    result++;
                else if(i % 7 == 0)
                    result++;
            }
            System.out.println(result);
        }
        in.close();
    }
}

发表于 2021-03-14 21:23:45 回复(0)
while True:
    try:
        num = int(input().strip())
        if 0 < num <= 30000:
            count_7 = 0
            for i in range(7,num+1):
                if i % 7 == 0&nbs***bsp;'7' in str(i):
                    count_7 += 1
            print(count_7)
    except:
        break


编辑于 2020-12-06 12:19:08 回复(0)
#include<stdio.h>
int main()
{
    int N,i,j,k,cnt;
    while(scanf("%d",&N)!=EOF)
    {
        if(N<7)
            printf("0\n");
        else
        {
            cnt=0;
            for(i=7;i<=N;i++)
            {
                if(i%7==0)
                {
                    cnt++;
                }
                else 
                {
                    k=i;
                    while(k>0)
                    {
                        if(k%10==7)
                        {
                            cnt++;
                            k=-1;
                        }
                        else
                        {
                            k=k/10;
                        }
                    }
                }
            }
            printf("%d\n",cnt);
        }
    }
    return 0;
}

发表于 2020-11-25 23:02:16 回复(0)
做了这么多,这还是第一次代码一次过,泪目!!!
先写一个函数,判断一个数有没有7,如果没有再判断是不是7的倍数。

#include "string.h"
#include <stdio.h>

int youqi(int a)
{
    int i=0;
    while (a>0)
    {
        if (a%10==7)  return 1;
        a=a/10;
    }
    return 0;
}

int main (void)
{
   int num;
    while (scanf("%d",&num)!=EOF)
    {
        int i=0;
        int p=0;
        if (num<7) printf("0\r\n");
        else 
        {
            for (i=7;i<=num;i++)
            {
                if (youqi(i)==1) 
                {
                    p++;
                }
                else if (i%7==0) p++;
                
            }
           printf("%d\r\n",p);
        }

    }
}


发表于 2020-08-24 20:14:37 回复(0)
while True:
    try:
        n = int(input())
        num = 0
        
        for i in range(1,n+1):
            if i % 7 == 0&nbs***bsp;'7' in str(i):
                num += 1
        print(num)

    except:
        break

发表于 2020-07-25 19:05:45 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int result = 0;
        for(int i=7;i <= n;++i)
            if(i%7==0 || (to_string(i).find('7')) != string::npos)
                result += 1;
        cout <<result << endl;
    }
    return 0;
}

发表于 2020-06-22 18:52:50 回复(1)
/**
 * 思路:直接遍历 7-n 中的每个数,若能被7整除,或者含有7即可
 */
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            System.out.println(selectSeven(scanner.nextInt()));
        }
    }

    /**
     * 挑7
     * @return
     */
    public static int selectSeven(int n) {
        int sum = 0;
        for (int i = 7; i <= n; i++) {
            if (i % 7 == 0 || String.valueOf(i).contains("7")){
                sum++;
            }
        }
        return sum;
    }
}

发表于 2020-04-22 09:50:05 回复(0)
import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            int n=sc.nextInt();
            int count=0;
            for(int i=7;i<=n;i++)
            {
                String num=""+i;
                if(i%7==0||num.contains("7"))
                {
                    count++;
                }              
            }
            System.out.println(count);
        }
    }

}
发表于 2020-02-21 10:01:08 回复(0)
#include <bits/stdc++.h>
using namespace std;

bool judge(int i){
    while(i > 0){
        if(i%10 == 7)
            return true;
        i /= 10;
    }
    return false;
}
int main(){
    int n,count;
    n=0;
    cin >> n;
    count = 0;
    for(int i = 1;i <= n;i++){
        if(i%7 == 0 || judge(i)){
            count++;
        }
    }
    cout << count <<endl;
}
有没有大佬帮我看一下为什么我自测可以通过,提交不通过(自测用的是提交不通过的测试用例)


发表于 2020-01-18 15:29:56 回复(1)