C语言怎么实现如下编程,谢谢: 1,2,3,4,5,6,7,8,9共9个数组成3个分数,每个数不重复使用,且分母均为两位数分子均为一位数,两个分数相加等于第三个
C语言怎么实现如下编程,谢谢: 1,2,3,4,5,6,7,8,9共9个数组成3个分数,每个数不重复使用,且分母均为两位数分子均为一位数,两个分数相加等于第三个分数,求出所有的组合
#include <stdio.h> #include <string.h> int main( void ) { for( unsigned n=0; n!=9*8*7*6*5*4*3*2*1; ++n ) { unsigned t, m[] = { 1,2,3,4,5,6,7,8,9 }; t = n/(8*7*6*5*4*3*2*1)%9; unsigned a = m[t]; memcpy( m+t, m+t+1, (8-t)*sizeof(unsigned) ); t = n/(7*6*5*4*3*2*1)%8; unsigned b = m[t]; memcpy( m+t, m+t+1, (7-t)*sizeof(unsigned) ); t = n/(6*5*4*3*2*1)%7; unsigned c = m[t]; memcpy( m+t, m+t+1, (6-t)*sizeof(unsigned) ); t = n/(5*4*3*2*1)%6; unsigned d = m[t]; memcpy( m+t, m+t+1, (5-t)*sizeof(unsigned) ); t = n/(4*3*2*1)%5; unsigned e = m[t]; memcpy( m+t, m+t+1, (4-t)*sizeof(unsigned) ); t = n/(3*2*1)%4; unsigned f = m[t]; memcpy( m+t, m+t+1, (3-t)*sizeof(unsigned) ); t = n/(2*1)%3; unsigned g = m[t]; memcpy( m+t, m+t+1, (2-t)*sizeof(unsigned) ); t = n/(1)%2; unsigned h = m[t]; memcpy( m+t, m+t+1, (1-t)*sizeof(unsigned) ); unsigned i = m[0]; if( a*(e*10+f)*(h*10+i) + d*(b*10+c)*(h*10+i) == g*(b*10+c)*(e*10+f) ) printf( "%d/%d%d + %d/%d%d = %d/%d%d\n", a,b,c,d,e,f,g,h,i ); } return 0; }