您的位置:首页 > 其它

uva 725 DIVISION (暴力枚举)

2015-12-04 17:39 417 查看
我的56MS

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
using namespace std;

#define MEM(a,v) memset (a,v,sizeof(a))
// a for address, v for value

#define max(x,y) ((x)>(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))

#define debug printf("!\n")

int num[15];
bool visited[15];

bool check(int a,int b)
{
MEM(visited,false);

int j;

for(j = 5;j>=1;j--)
{
int tmp = a%10;
num[j] = tmp;
a/=10;
}
for(j = 10;j>=6;j--)
{
int tmp = b%10;
num[j] = tmp;
b/=10;
}

for(j = 1;j<=10;j++)
{
if(visited[num[j]])
return false;
visited[num[j]] = true;
}
return true;

}

int main()
{
int i,n,j,k,T=0;
while(~scanf("%d",&n) && n)
{
if(T++)
printf("\n");
bool find = false;
for(i = 1234;i<=98765;i++)
{
if(i%n==0)
{
int m = i/n;
if(check(i,m))
{
find =true;
for(j = 1;j<=5;j++)
printf("%d",num[j]);
printf(" / ");
for(j = 6;j<=10;j++)
printf("%d",num[j]);
printf(" = %d\n",n);
}
}
}
if(!find)
printf("There are no solutions for %d.\n",n);
}

return 0;
}


但要特别提一下这位仁兄写的,有狠多值得学习的地方
http://blog.csdn.net/mobius_strip/article/details/38735773
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int used[10];

int judge( int a, int b )
{
//对于判断是否有重复的数字,可以用一个used数组
//将用过的项置为1

if ( b > 98765 ) return 0;
for ( int i = 0 ; i < 10 ; ++ i )
used[i] = 0;
if ( a < 10000 ) used[0] = 1;
while ( a ) {
used[a%10] = 1;
a /= 10;
}
while ( b ) {
used[b%10] = 1;
b /= 10;
}
int sum = 0;
for ( int i = 0 ; i < 10 ; ++ i )
sum += used[i];
return (sum == 10);
}

int main()
{
int n, T = 0;
while ( ~scanf("%d",&n) && n ) {
if ( T ++ ) printf("\n");
int count = 0;
for ( int i = 1234 ; i < 100000 ; ++ i ) {
if ( judge( i, i*n ) ) {
printf("%05d / %05d = %d\n",i*n,i,n);
//可以用%05d填充,就不用数组来保存数字了
count ++;
}
}
if ( !count )
printf("There are no solutions for %d.\n",n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: