码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-06 11:14:14      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:c++   暴力打表   


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:b
using 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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:c++   暴力打表   

原文地址:http://blog.csdn.net/stffer/article/details/47312039

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!