如何在一堆数字中找出几个数,这几个数的和能够相加得一个固定值
发布网友
发布时间:2022-04-28 22:30
我来回答
共4个回答
热心网友
时间:2023-09-19 03:13
In[6]:= a = {1161, 2538, 4480, 5925, 12166, 12693, 14279, 15507,
24624, 28844, 38946, 45114, 47222, 55747, 63859, 68862, 74672,
89170, 97914, 102866, 103821, 115338, 163487,
276068}; mb = 487235; aa =
Table[ToExpression[StringJoin["a", ToString[i]]], {i, 1, Length[a]}];
bb = a.aa; t =
Solve[Flatten[{Total[bb] == mb, Map[{# >= 0, # <= 1} &, aa]}], aa,
Integers]; Length[t]
aa /. t
Out[7]= 16
Out[8]= {{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1}, {0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1,
1, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0}, {0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1, 1, 0, 1, 0, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0}, {1, 0, 0, 0, 1, 0, 0, 1, 1, 1,
0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 1, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0}, {1, 0, 0, 1, 1, 0,
0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0}, {1, 0, 1, 0,
0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0}, {1, 0,
1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,
0}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
0, 0, 0}, {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0}, {1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0,
1, 1, 0, 1, 0, 0, 0}, {1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
1, 0, 0, 0, 1, 1, 1, 0, 0}, {1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0}, {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0}}
用Mathematica解出来有16组解,如果是4张,那就是第一个解。运算过程中我把数据全放大了100倍,用整数来运算。
热心网友
时间:2023-09-19 03:14
遍历即可,我用matlab编的简单程序,其他语言类似
A=[11.61,25.38,44.8,59.25, 121.66,126.93 ,142.79,155.07,246.24,288.44,389.46,451.14,472.22,557.47,638.59,688.62,746.72,891.7,979.14,1028.66,1038.21,1153.38,1634.87,2760.68];
n=length(A); %求A中元素的个数
for i=1:n-3
for j=i+1:n-2
for k=j+1:n-1
for l=k+1:n
if A(i)+A(j)+A(k)+A(l)==4872.35 %判断条件
[A(i) A(j) A(k) A(l)]
end
end
end
end
end
我这里预设的和是4872.35
程序运行的结果是44.8、1028.66、1038.21、2760.68
你要的和无解
热心网友
时间:2023-09-19 03:14
C++版代码如下,做了一点拓展,可以求出通解
#include<iostream>
#include<cstdio>
using namespace std;
double a[1000], b[1000], c[1000];
double x ;
int n,m ;
void f(int i, int l){
if(i > n || l > m)return;
b[l] = a[i];
c[l] = i ;
if(l == m){
double sum = 0;
for(int k = 1; k <= m; k++)sum += b[k];
if(sum == x){
for(int k = 1 ; k <= m ; k++){
cout << b[k];
if(k < m)cout << " + ";
}
cout << " = " << x << endl << "这是";
for(int k = 1 ; k <= m ; k++){
cout << "第 " << c[k] << " 个数" ;
if(k < m)cout << "、";
}
cout << "相加。" << endl;
}
}
else{for(int y = i + 1; y <= n - m + l + 1 ; y ++)f(y, l+1);}
return;
}
int main(){
cout << "请输入数字个数(完成后回车):";
cin >> n;
cout << "请输入这"<<n<<"个数,每输入一个间隔一个空格(所有数字都输入完成后回车):\n" ;
for(int i = 1; i <= n ; i++)cin >> a[i];
cout <<"请输入需要选出多少个数相加(完成后回车)\n";
cout <<"(若需要的是选出任意个数的,请输入0然后回车):\n";
cin >> m ;
cout << "请输入所需得到的固定值(完成后回车):" ;
cin >> x ;
if(m == 0){
for(m = 1; m <= n; m++){
cout << "\n选出" << m << "个数相加,所得到的组合有:\n";
for(int i = 1; i <= n-m+1; i++)f(i,1);
}
}
else{
cout << "\n选出" << m << "个数相加,所得到的组合有:\n";
for(int i = 1; i <= n-m+1; i++)f(i,1);
}
return 0;
}
热心网友
时间:2023-09-19 03:15
你要的和不存在,。java版如下:
public class test {
public static void main(String[] args) {
Double[] d={11.61,25.38,44.8,59.25,121.66,126.93,142.79,155.07,246.24,288.44,389.46,451.14,472.22,557.47,638.59,688.62,746.72 , 891.7,979.14,1028.66,1038.21,1153.38,1634.87,2760.68};
double s=0.0;
int count=0;
double a=5560.54;
for(int i=0;i<24;i++) {
for(int j=1;j<24;j++) {
for(int x=2;x<24;x++) {
for(int y=3;y<24;y++) {
s=d[i]+d[j]+d[x]+d[y];
if(s==a){
System.out.print(d[i]+",");
System.out.print(d[j]+",");
System.out.print(d[x]+",");
System.out.println(d[y]);
System.out.println("------");
}
count=count+1;
}
}
}
}
System.out.println(count);
}
}