簡單題,一個數字(number)的各個位(digit)上的數字如果有重複,則視爲有重複數位的數。給定兩個數字0<=N<=M<= 5000,求N和M之間沒有重複數位的數有多少個。
由於本身數字也不多,直接開個數組d,把每個數i是否是重複數位的結果存儲到d[i],然後再另一個數組s,直接累加結果,答案也就很快出來了。
python版本代碼
s = [0 for i in range(5001)]
d = [0 for i in range(5001)]
def IsRepeat(num):
a = [0 for i in range(10)]
while num > 0:
x = num % 10
if a[x] == 0:
a[x] = 1
else:
return False
num //= 10
return True
for i in range(5001):
if i == 0:
continue
else:
if IsRepeat(i) == True:
d[i] = 1
s[i] = s[i-1] + d[i]
try:
while True:
N,M = input().split()
N = int(N)
M = int(M)
print(s[M]-s[N]+d[N])
except Exception as e:
pass
C/C++版本代碼
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//#define ZANGFONG
const int maxn = 5050;
int d[maxn],s[maxn];
int a[10];
bool isrepeat(int x)
{
int i;
memset(a,0,sizeof(a));
while(x)
{
i = x%10;
if(a[i]==0) a[i]++;
else return false;
x /= 10;
}
return true;
}
int main()
{
#ifdef ZANGFONG
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ZANGFONG
int N,M,i;
for(i = 1; i <= 5000; i++)
{
if(isrepeat(i)) d[i] = 1;
s[i] = d[i] + s[i-1];
}
while(scanf("%d%d\n",&N,&M)!= EOF)
{
printf("%d\n",s[M]-s[N]+d[N]);
}
return 0;
}