本文主要是介绍利用9个数组组成三组数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用1、2、3、4、5、6、7、8、9九个数字拼成一个九位数(每个数字恰好用一次),使得它的前三位、中间三位、最后三位的比值是1 : 2 : 3。例如192384576就是一个合法的解,因为192 : 384 : 576 = 1 : 2 : 3。
#include <iostream>
using namespace std;void sulotion()
{int a[9]={1,2,3,4,5,6,7,8,9};int flag[10]={0};bool b=true;int temp1[3],temp2[3];for(int i=0; i<3; ++i){flag[i+1]=1;for ( int j=0; j<9; ++j ){if (i==j) continue;flag[j+1]=1;for ( int k=0; k<9; ++k ){if (i==k||j==k) continue;int num1 = 2*(a[i]*100+a[j]*10+a[k]);int num2 = (num1>>1)*3;if (num1>=1000 || num2>=1000)continue;flag[k+1]=1;for ( int index=0; index<3; ++index ){temp1[index]=num1%10;temp2[index]=num2%10;if (flag[temp1[index]] ) b=false;flag[temp1[index]]=1;if (flag[temp2[index]]) b=false;flag[temp2[index]]=1;num1/=10;num2/=10;if ( index ==2 && b ){cout<<( 1*(a[i]*100+a[j]*10+a[k]))<<" "<< 2*(a[i]*100+a[j]*10+a[k])<<" "<< 3*(a[i]*100+a[j]*10+a[k])<<endl;}}for ( int index = 0; index<3; index++ ){flag[temp1[index]]=0;flag[temp2[index]]=0;}flag[i+1]=1;flag[j+1]=1;b = true;flag[k+1]=0; flag[0]=1;}flag[j+1]=0;}flag[i+1]=0;}
}int main()
{sulotion();
}
这篇关于利用9个数组组成三组数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!