问题 1526: [蓝桥杯][算法提高VIP]排列式

题目

解题思路:

1.对1~9进行全排列;

2.经由分析,结果只能为4位数(等号左边),等号右边有两种情况(1位数*4位数)或者(2位数*3位数);

3.对每种情况进行判断。

参考思路:http://www.dotcpp.com/blog/56469.html

源码附上:

#include <iostream>
#include <algorithm>
using namespace std;
int A[]={1,2,3,4,5,6,7,8,9};

int change(int a,int b)
{
	int num=0;
	for(int i=a;i<=b;i++)
	{
		num=num*10+A[i];
	}
	return num;
}

void judge(int A[])
{
	int left=change(0,3);
	for(int i=4;i<=5;i++)
	{
		int right1=change(4,i);
		int right2=change(i+1,8);
		if(left==right1*right2)
		{
			cout<<left<<" = "<<right1<<" x "<<right2<<endl;
		}
	}
} 

int main()
{
	do{
		judge(A);
	}while(next_permutation(A,A+9));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Exaggeration08/article/details/87919593