描述
Count how many numbers do not contain 4 or 7 in the N numbers from 1 to N.
输入
Each test case starts with 1 <= N <= 1,000,000. The input is terminated by the end of file (EOF).
输出
For each test case, print how many numbers from 1 to N do not contain 4 or 7.
输入样例 1
5 10 20
输出样例 1
4 8 16
思路:
写在最前:好久不上oj,再写题的时候注意输入输出格式:
1.题目是连续输入多组数据,所以用while(cin>>n)控制输入;
2.每行要输出换行符不然格式不对。
这个题有一种思路是把数字转成string再去遍历每个字符来判断,别这么写,时间过不了!!
就对数字进行按位处理(这是本题的知识点,如何获取数字的每一位数)再判断当前位数是不是4或者7,是的话就n--,最后输出n即可。
代码:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool check(int x)
{
while(x>0)
{
if(x % 10 == 4 || x % 10 == 7 )
return true;
x /= 10;
}
return false;
}
int main()
{
int n,ans;
while (cin >> n)
{
ans = n;
for (int i = 1; i <= n; i++)
{
if(check(i) == 1)
ans--;
}
cout << ans << endl;
}
return 0;
}