UVA 725 division【暴力枚举】

【题意】:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果。如果没有找到则输出“There are no solutions for N.”。这里2<=n<=79。

【分析】:

1.因为n>=2,且abcde=fghij×n,满足abcde>fghij。若a=0,则fghij的最小值为12345,abcde<fghij,矛盾。所以a≠0。

2.因为a≠0,所以12345<=abcde<=98765,01234<=fghij。

3.因为2≤n,且abcde≤98765,那么fghij = abcde/n,得fghij≤98765/2=49382,所以01234≤fghij≤49382。

4.因为12345≤abcde≤98765,且01234≤fghij≤49382,所以用fghij进行枚举范围比较小。(这是在任意的n的条件下得出的结论)

5.对于给定的n,因为abcde≤98765,那么fghij = abcde/n,得fghij≤98765/n。结论:01234≤fghij≤98765/n。

【代码】:

#include<bits/stdc++.h>
using namespace std;
int v[15];

int check(int x,int y)
{
    memset(v,0,sizeof(v));
    for(int i=1;i<=5;i++)
    {
        v[x%10]++;
        v[y%10]++;
        if(v[x%10]>1||v[y%10]>1) return 0;
        x/=10;
        y/=10;
    }
    return 1;
}

int main()
{
    int n,t=0,f;
    while(~scanf("%d",&n),n)
    {
        f=0;
        if(t++) printf("
");
        for(int i=1234;i<=100000/n;i++)
        {
            if(check(i,i*n))
            {
                printf("%05d / %05d = %d
",i*n,i,n);
                f=1;
            }
        }
        if(!f) printf("There are no solutions for %d.
",n);
    }
}

  

原文地址:https://www.cnblogs.com/Roni-i/p/8058166.html