HDU5327(基础题)

来源:互联网 发布:淘宝大码孕妇装排行 编辑:程序博客网 时间:2024/06/07 18:32

题目:
G - Olympiad
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

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] (ab). Please be fast to get the gold medal!

Input
The first line of the input is a single integer T (T1000), 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 1ab100000.

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

Sample Input
2
1 10
1 1000

Sample Output
10
738

题意:
找出所给范围的漂亮数(每个数位上的数互不相同)

思路:
数据范围会很大,暴力的话会重复多次求解,那么打表,表中每个arr[i]记录前i个数中有多少个漂亮数,并且判断i是不是漂亮数,一直累加,求解时区间相减,注意还有判断区间起始值a是否为漂亮数,并且加上。

代码:

#include <iostream>#include <cstring>const int maxn = 100000+10;int arr[maxn];int node[10];using namespace std;int check(int a){    memset(node, 0, sizeof(node));    while(a){        node[a%10]++;        if(node[a%10]>1)            return 0;        a = a/10;    }    return 1;}void printList(){    memset(arr, 0, sizeof(arr));    for(int i=0; i<=100010; i++){        if(check(i)){            //cout<<"i is:"<<i<<endl;            arr[i] = arr[i-1] + 1;}        else            arr[i] = arr[i-1];    }}int main(){    int n, a, b;    while(cin>>n){        printList();        for(int i=0; i<n; i++){            cin>>a>>b;            cout<<arr[b]-arr[a]+check(a)<<endl;        }    }    return 0;}

check()函数判断是否为漂亮数,printList()函数打表,其中check()函数返回值设为int,返回值为1或0,可以在最后区间相减的时候判断区间开始的数是否为漂亮数。

0 0
原创粉丝点击