除法(Division,UVa 725)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/A

题意:

        输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a~j恰好为数字0~9的一个排列(可以与前导0),2<=n<=79。找出所有满足条件的表达式,若无,则输出“There are no solutions for n.”。不同案例输出用空白行分离。

案例:

        Sample Input

        61

        62

        0

        Sample Output

        There are no solutions for 61.

 

        79546 / 01283 = 62

        94736 / 01528 = 62

分析:

       不需要枚举所有排列,只需枚举fghij就可算出abcde,然后判断所有数字是否不同即可。
源代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int b[10];
 5 int test(int m,int n)
 6 {   int z=0;
 7     if(m>98765) return 0;
 8     b[0]=m/10000;//将参数值的个十百千万位数值传递存于数组b
 9     b[1]=m/1000%10;
10     b[2]=m/100%10;
11     b[3]=m/10%10;
12     b[4]=m%10;
13     b[5]=n/10000;
14     b[6]=n/1000%10;
15     b[7]=n/100%10;
16     b[8]=n/10%10;
17     b[9]=n%10;
18     sort(b,b+10);//数组b进行排序
19     for(int j=0;j<10;j++)//判断参数值个十百千万位是否符合要求
20         if(b[j]==j) z++;
21     if(z==10) return z;//判断参数值是否由0~9构成且不重复
22     else return 0;
23 }
24 int main()
25 {
26     int N,i,cnt=0,count;
27     while(scanf("%d",&N)&&N>=2&&N<=79)
28     {   
29         count=0;
30         if(cnt++) printf("\n");//格式控制
31         for(i=1234;i<=98765;i++)
32             if(test(i*N,i))//测试数据是否符合要求
33             {  printf("%05d / %05d = %d\n",i*N,i,N);
34                ++count;
35             }
36         if(count==0)//判断能否产生符合要求的数据 
37            printf("There are no solutions for %d.\n",N);
38     }
39     return 0;
40 }

 

posted @ 2015-07-27 19:06  ~瞬间*  阅读(258)  评论(0编辑  收藏  举报