UVA 725 Division(hash+枚举)

题目大意:

  就是说给你一个abcde/fghij = n的表达式,给你一个n,让你求出有多少个这样的式子。

解题思路:

  最为简单的枚举了,要注意到我们只需要枚举出来fghij就可以了,因为abcde=fghij*n,这样的话,时间复杂度由10!就

降低到了5!,在枚举结束后,我们只需要判断0-9这些数字是否都出现了且仅出现一次即可。然后对于超过5位的数字,我们

直接break掉。

  这道题的输出一定要注意那个前导0,,一开始忘记了,WA了两次,然后果断用setw(5)和setfill('0')给搞定了。

  这条语句也是可以搞定的 printf("%d / %05d = %d ",tt,t,n);

代码:

  

# include<cstdio>
# include<iostream>
# include<algorithm>
# include<functional>
# include<cstring>
# include<string>
# include<cstdlib>
# include<iomanip>
# include<numeric>
# include<cctype>
# include<cmath>
# include<ctime>
# include<queue>
# include<stack>
# include<list>
# include<set>
# include<map>

using namespace std;

const double PI=4.0*atan(1.0);

typedef long long LL;
typedef unsigned long long ULL;

# define inf 999999999

int n;
int num[10];


int check ( int x,int y )
{
     memset(num,0,sizeof(num));
    int a = x;
    int b = y;
    for ( int i = 0;i < 5;i++ )
    {
        num[a%10]++;
        num[b%10]++;
        a/=10;
        b/=10;
    }
    for ( int i = 0;i < 10;i++ )
    {
        if ( num[i]!=1 )
            return 0;
    }
    return 1;
}

int main(void)
{
    int icase = 1;
    while ( cin>>n )
    {
        if ( n==0 )
            break;
        if ( icase > 1 )
                cout<<endl;
        int flag = 0;
        for ( int t = 1234;;t++ )
        {
            int tt = t*n;
            if ( tt >= 100000 )
                break;
            if ( check(tt,t) )
            {
                flag = 1;
                printf("%d / %05d = %d
",tt,t,n);
                 //printf("%d / %d = %d
",tt,t,n);
            }
        }
        if ( !flag )
        {
            printf("There are no solutions for %d.
",n);
        }
        icase++;
    }


    return 0;
}
View Code
原文地址:https://www.cnblogs.com/wikioibai/p/4391353.html