hdu 5327 Olympiad (多校联赛) ——模拟

来源:互联网 发布:淘宝千纸鹤什么档次 编辑:程序博客网 时间:2024/06/10 12:49


here


题意:给你两个数,让你求出这两个数之间有多少个数,这个数满足他的每一位的数都不相同。例如 113 不满足 143 满足。


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] (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.
 

我的思路直接暴力打表,都是打表,但是想法太有差距了,从这两段代码中可以看出。自省~~


code:

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<math.h>#define max1(a,b) a>b?a:busing namespace std;bool is(int i){    int a,b,c,d,e;    a=i%10;    b=i%100/10;    c=i%1000/100;    d=i%10000/1000;    e=i%100000/10000;    if(e==0&&d==0&&c==0&&b==0)    {        return true;    }    else if(e==0&&d==0&&c==0&&b!=0)    {        if(b!=a)            return true;    }    else if(e==0&&d==0&&c!=0)    {        if(a!=b&&a!=c&&b!=c)            return true;    }    else if(e==0&&d!=0)    {        if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)            return true;    }    else if(e!=0)    {        if(a!=b&&a!=c&&a!=d&&a!=e&&                b!=c&&b!=d&&b!=e&&                c!=d&&c!=e&&                d!=e)            return true;    }    return false;}int a[100005];int main(){    a[1]=1;    for(int i=2; i<=99999; i++)    {        if(is(i))            a[i]=a[i-1]+1;        else            a[i]=a[i-1];    }    a[100000]=a[99999];    int t;    scanf("%d",&t);    while(t--)    {        int c,b;        scanf("%d%d",&c,&b);        printf("%d\n",a[b]-a[c-1]);    }}

优秀代码:这就是思想的差距。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int temp[10];int ans[100001];int i,j,n,a,b;int fun1(int x)//用于判断数字是否满足条件{    int mod;    memset(temp,0,sizeof(temp));    while(x != 0)    {        mod = x % 10;        if(temp[mod])            return 0;        else            temp[mod]++;        x /= 10;    }    return 1;}int main(){    for(i = 1;i < 100001;i++)    {        if(fun1(i))            ans[i] = ans[i-1] + 1;        else            ans[i] = ans[i-1];    }    scanf("%d",&n);   while(n--)    {        scanf("%d%d",&a,&b);        printf("%d\n",ans[b] - ans[a-1]);    }    return 0;}





0 0
原创粉丝点击