1、先来实现计算某个数的位数下面是代码:
#include<iostream>
using namespace std;
int main() {
int num = 0;
while (cin >> num) {
int m = 0;
int flag = 1;
while (flag) {
num = num / 10;
m++;
if (num == 0) {
flag = 0;
}
}
cout << m << endl;
}
return 0;
}
Simple Count
描述
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
代码实现:
#include<iostream>
using namespace std;
int main() {
int num = 0;
while (cin >> num) {
int count = 0;
int m = 0;//m 是这个数的位数
int clone_num = num;
int flag = 1;
while (flag) {
num = num / 10;
m++;
if (num == 0) {
flag = 0;
}
}
int mod = 0;//余数
for (int i = 1; i <= clone_num; i++) {
for (int j = 0; j < m; j++) {
int clone_i = i;//复制一个i因为等下要用i来求其他位的数字,同时在外层循环i又要自增
mod = clone_i % 10;
if (mod == 4 || mod == 7) {
count++;
break;//找到最低的一位数是4或者7就直接退出循环
}
else
clone_i = clone_i / 10;
}
}
cout << clone_num-count << endl;
}
return 0;
}