算法入门经典(第七章)

7.1.1 简单枚举

#include 
#include 
#include 
using namespace std;

int main()
{
	//freopen("out.txt","w",stdout);
	int num1[50] = {0};
	int num2[50] = {0};
	int n,k,ix,sx,j,l,i;
	bool flag;
	cin>>n;
	for( i = 01234; i < 99999; i++ )
	{
		ix = i;
		k = 0;
		while( ix % 10)
		{
			num1[k++] = ix % 10;
			ix = ix / 10;
		}
		if(k==4) num1[4] = 0;
		k++;
		if(k!=5) continue;

		flag = false;
		for(j=0;j<5;j++)
		{
			for(l=j+1;l<5;l++)
			{
				if(num1[j]==num1[l])
				{
					flag = true;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) continue;

		sx = n * i;
		k = 0;
		while( sx % 10)
		{
			num2[k++] = sx % 10;
			sx = sx / 10;
		}

		flag = false;
		for(j=0;j<5;j++)
		{
			for(l=j+1;l<5;l++)
			{
				if(num2[j]==num2[l])
				{
					flag = true;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) continue;

		if(k==5)
		{
			flag = false;
			for(j=0;j<5;j++)
			{
				for(l=0;l<5;l++)
				{
					if(num1[j]==num2[l])
					{
						flag = true;
						break;
					}
				}
				if(flag) break;
			}
			if(flag) continue;

			printf("%05d/%05d=%d\n",n*i,i,n);
		}
	}
}


转载于:https://www.cnblogs.com/lgh1992314/archive/2012/11/18/5835303.html

你可能感兴趣的:(算法入门经典(第七章))