HDU 5327 Olympiad

来源:互联网 发布:unity3d敌人追踪主角 编辑:程序博客网 时间:2024/05/18 06:29

http://acm.hdu.edu.cn/showproblem.php?pid=5327

Problem Description
You are one of the competitors of the Olympiad in numbers. The problem of this year relates to beatiful numbers. One integer is called beautiful if and only if all of its digitals are different (i.e. 12345 is beautiful, 11 is not beautiful and 100 is not beautiful). Every time you are asked to count how many beautiful numbers there are in the interval [a,b] (a≤b). Please be fast to get the gold medal!

Input
The first line of the input is a single integer T (T≤1000), indicating the number of testcases.

For each test case, there are two numbers a and b, as described in the statement. It is guaranteed that 1≤a≤b≤100000.

Output
For each testcase, print one line indicating the answer.

Sample Input
2
1 10
1 1000

Sample Output
10
738

解题思路:
1.总方法为预处理法,sum=sum[b]-sum[a-1];
2.如何求sum?用函数求解,用一个大小为10的数组来存储0到9的数字,出现一次+1,如果原来不为0,则返回0,如果是之前没出现的就返回1.然后sum[i]=sum[i-1]+0/1;

代码:

#include<stdio.h>#include<string.h>#define N 100000int benum(int a){    int w[10];    memset(w,0,sizeof(w));   while(a)   {      if(w[a%10]!=0)   return 0;      w[a%10]++;      a=a/10;   }   return 1;}int main(){    int i,a[N],T,b,c,d;    a[1]=benum(1);    for(i=2;i<=100000;i++)        a[i]=a[i-1]+benum(i);    scanf("%d",&T);    while(T--)    {        scanf("%d %d",&b,&c);        d=a[c]-a[b-1];        printf("%d\n",d);    }    return 0;}
0 0
原创粉丝点击