UVA

题目链接https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666

题目大意:

输入一个n,求出所有abcde/fghij=n的情况。

思路:枚举所有情况。因为枚举的数字是从01234~98765,时间是 不可能超限的。

ps:注意格式,搞不好就PE

这题格式好怪,反正我是不知道为啥是这种格式。。。

代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int main ()
 7 {
 8     int n;
 9     int T=0;
10     while(cin>>n)
11     {
12         if(n==0)    break;
13         if(T++) cout<<endl;
14         int vis[15];
15         bool flag=false;
16         for (int i=1234;i<=98765;i++)
17         {
18             bool fl=true;
19             memset(vis,0,sizeof(vis));
20             int tmp=i*n;
21             if(tmp>98765)
22                 break;
23             int a,b,c,d,e,f,g,h,x,j;
24             a=i/10000;
25             vis[a]++;
26             b=(i/1000)%10;
27             vis[b]++;
28             c=((i/100)%100)%10;
29             vis[c]++;
30             d=(((i/10)%1000)%100)%10;
31             vis[d]++;
32             e=i%10;
33             vis[e]++;
34             f=tmp/10000;
35             vis[f]++;
36             g=(tmp/1000)%10;
37             vis[g]++;
38             h=((tmp/100)%100)%10;
39             vis[h]++;
40             x=(((tmp/10)%1000)%100)%10;
41             vis[x]++;
42             j=tmp%10;
43             vis[j]++;
44             for(int k=0;k<10;k++)
45             {
46                 if(vis[k]>1)
47                     fl=false;
48             }
49             if(fl)
50             {
51                 if(tmp<10000&&i<10000)
52                     cout<<'0'<<tmp<<" / "<<'0'<<i<<" = "<<n<<endl;
53                 else if(tmp<10000)
54                     cout<<'0'<<tmp<<" / "<<i<<" = "<<n<<endl;
55                 else if(i<10000)
56                     cout<<tmp<<" / "<<'0'<<i<<" = "<<n<<endl;
57                 else
58                     cout<<tmp<<" / "<<i<<" = "<<n<<endl;
59                 flag=true;
60             }
61         }
62         if(!flag)
63             cout<<"There are no solutions for "<<n<<'.'<<endl;
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/ISGuXing/p/7236510.html