算法提高 寻找三位数

/*
问题描述
将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成
1:2:3的比例,试求出所有满足条件的三个三位数。
例 如:三个三位数192,384,576满足以上条件。
输入格式
无输入文件
输出格式
输出每行有三个数,为满足题设三位数。各行为满足要求的不同解。
*/

#include <stdio.h>

int you9geshu( int [] , int) ;
void tongji(int [],int);
int heli(int [],int);
void shuchu(int [],int);

int main(void) 
{
	int s[3];
	for ( s[0] = 123;
		  s[1] = 2 * s[0] , (s[2] = 3 * s[0]) < 1000 ; 
		  s[0]++                                     )
	{
		
		if ( you9geshu( s , 3 ) != 2 )
		{
			shuchu( s , 3 );
		}
	}	
	return 0;
}

void shuchu(int s[],int n)
{
	while ( n -- > 0 )
	{
		printf("%d ", * s ++ ) ;
	}
	putchar('\n');
}

int heli(int sz[],int n)
{
	while ( -- n > 0 )
	{
		if ( sz[n] != 1 )
		{
			return 2;
		}
	}
}

int you9geshu( int s[] , int n ) 
{
	int shuzi[10] = { 0 };
	while ( n -- > 0 )
	{
		tongji(shuzi, * s ++ );
	}
	return heli(shuzi,10);
}

void tongji(int sz[],int d)
{
	do
	{
		sz[d%10]++;
	}
	while ( (d/=10) != 0 );
}

猜你喜欢

转载自blog.csdn.net/qq_41353167/article/details/81836503