#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
double array[28]={
1394.87,1264.10,929.91,464.96,2817.53,1735.61,209.23,
6659.64,45769.23,30512.82,1220.51,4183.16,3773.56,9938.46,
2767.31,5055.07,52341.59,13289.05,45769.23,9805.08,9310.77,
3051.28,2589.23,1307.69,1278.63,1525.64,958.97,876.88 };
double result=73001.83 ;
int i,j;
for (i = 1; i < 1 << 28; i++)//从1循环到2^28
{
double sum=0;
char o_str[1024]={0};
char temp[32];
for ( j = 0; j < 28; j++)
{
if ((i & 1 << j) != 0)//用i与2^j进行位与运算,若结果不为0,则表示第j位不为0,从数组中取出第j个数
{
sum += array[j];
sprintf( temp , "%.2lf" , array[j] );
if ( o_str[0] )
strcat( o_str , " + " );
strcat( o_str , temp );
}
if ( sum >= result )
break ;
}
if ( fabs(sum-result) <= 0.005 ) //两数相等
{
printf("%s = %.2lf\n" , o_str , result );
break ; //找到了,就退出。如果要找多组,则这里要屏蔽break;
}
}
return 0;
}
高手写的,怕忘记留下来看看。