华为oj : 挑7

来源:互联网 发布:javascript 代码规范 编辑:程序博客网 时间:2024/04/29 20:00

描述

输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数

知识点循环运行时间限制0M内存限制0输入

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

输出

不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17.

样例输入20样例输出3思想:采用一个数一个数判断的方法

#include <iostream>  using namespace std;int numOfSeven(int N){int count=0;int temp;for(int i=6;i<=N;i++){if (i%7==0) count++;else             {                 int temp = i;                 while (temp > 0)                 {                     if (temp % 10 == 7) //再找含有7的数用求余数的方法                    {                         //printf("%d\n",i);                        count++;                       break;                     }                     temp = temp / 10;                 }             }    }return count;}int main(){int N;cin>>N;cout<<numOfSeven(N);}


0 0