2002 - The Almost Lucky Numbers
John and Brus believe that the digits 4 and 7 are lucky and all others are not. According to them, an almost lucky number is a number that contains at most one non-lucky digit in its decimal representation. Return the total number of almost lucky numbers between a and b, inclusive.
题目输入
Each line contains two integers a,b<=1,000,000
题目输出
For each line of input,output the total number of almost lucky numbers between a and b, inclusive.
输入/输出样例
题目输入
4 7 8 19
题目输出
4 4
C语言解答
#include<stdio.h> int main() { int a, b, t; while(scanf("%d%d", &a, &b) != EOF){ int sum = 0; for(int i = a; i <= b; i++){ t = i; int count = 0 ; while(t){ if((t % 10 != 4 ) && (t % 10 != 7)) count++; t = t / 10; } if(count < 2) sum++; } printf("%d\n", sum); } return 0; }
C++解答
#include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <cstdio> #include <map> using namespace std; vector<int>v; int vis[10]; bool is_ok(int n){ memset(vis,0,sizeof(vis)); while(n){ vis[n%10]++; n/=10; } int ans=0; for(int i=0;i<=9;i++){ if(i==4 || i==7) continue; else ans+=vis[i]; } if(ans>1) return false; return true; } int main() { v.clear(); for(int i=0;i<=1000000;i++){ if(is_ok(i)) v.push_back(i); } int a,b; while(cin>>a>>b){ if(a>b) swap(a,b); while(!is_ok(a)) a++; while(!is_ok(b)) b--; cout<<lower_bound(v.begin(),v.end(),b)- lower_bound(v.begin(),v.end(),a)+1<<endl; } return 0; }