夹娃多项式展开
突然发神经想到写个多项式展开的代码,想了一下发现还有点绕,于是...
首先研究下cn2的形式
private int run97_2(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
for (int k = j + 1; k < a.length; k++) {
s += a[i] * a[j] * a[k];
}
}
}
return s;
}
再研究下cn3的形式
private int run97_3(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
for (int k = j + 1; k < a.length; k++) {
for (int m = k + 1; m < a.length; m++) {
s += a[i] * a[j] * a[k] * a[m];
}
}
}
}
return s;
}
如果将这个形式改写成递归的形式,变量会出现在递归方法外,这段代码被我改掉了,也不复杂有兴趣的可以自己写下,
于是将这种形式改写为如下形式
private int run97_5(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
int si = 0;
for (int j = i + 1; j < a.length; j++) {
int sk = 0;
for (int k = j + 1; k < a.length; k++) {
int sm = 0;
for (int m = k + 1; m < a.length; m++) {
sm += a[m];
}
sk += sm * a[k];
}
si += sk * a[j];
}
s += si * a[i];
}
return s;
}
这样变量就全在这个方法内了,
根据这种形式的代码改写出来的递归形式如下
private int run97_4_1(int[] a, int[] idx, int lvcurrent, int lvall) {
int s = 0;
if (lvcurrent == lvall - 1) {
// System.out.println("a:"+idx[lvcurrent]);
for (int i = idx[lvcurrent-1] + 1; i < a.length; i++) {
s += a[i];
// s+=a[idx[lvcurrent-1]]*a[idx[i]];
}
return s;
} else {
// System.out.println("b:"+idx[lvcurrent]);
for (int i = idx[lvcurrent-1] + 1; i < a.length; i++) {
idx[lvcurrent] = i;
s += a[i] * run97_4_1(a, idx, lvcurrent + 1, lvall);
}
return s;
}
}
在matlab上测试一下结果对比一下
夹娃:
private void run97() {
int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
System.out.println(run97_4(a, 2));
System.out.println(run97_4(a, 3));
System.out.println(run97_4(a, 4));
System.out.println(run97_4(a, 5));
System.out.println(run97_4(a, 6));
System.out.println(run97_4(a, 7));
}
private int run97_4(int[] a, int lvall) {
int[] idx = new int[lvall];
idx[0]=-1;
return run97_4_1(a, idx, 1, lvall);
}
public static void main(String[] args) {
Test1 t = new Test1();
t.run97();
}
matlab
syms x y;
y=sym((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6));
expand(y)
一致.恩,还马马虎虎
(*..*)
突然发神经想到写个多项式展开的代码,想了一下发现还有点绕,于是...
首先研究下cn2的形式
private int run97_2(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
for (int k = j + 1; k < a.length; k++) {
s += a[i] * a[j] * a[k];
}
}
}
return s;
}
再研究下cn3的形式
private int run97_3(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
for (int k = j + 1; k < a.length; k++) {
for (int m = k + 1; m < a.length; m++) {
s += a[i] * a[j] * a[k] * a[m];
}
}
}
}
return s;
}
如果将这个形式改写成递归的形式,变量会出现在递归方法外,这段代码被我改掉了,也不复杂有兴趣的可以自己写下,
于是将这种形式改写为如下形式
private int run97_5(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
int si = 0;
for (int j = i + 1; j < a.length; j++) {
int sk = 0;
for (int k = j + 1; k < a.length; k++) {
int sm = 0;
for (int m = k + 1; m < a.length; m++) {
sm += a[m];
}
sk += sm * a[k];
}
si += sk * a[j];
}
s += si * a[i];
}
return s;
}
这样变量就全在这个方法内了,
根据这种形式的代码改写出来的递归形式如下
private int run97_4_1(int[] a, int[] idx, int lvcurrent, int lvall) {
int s = 0;
if (lvcurrent == lvall - 1) {
// System.out.println("a:"+idx[lvcurrent]);
for (int i = idx[lvcurrent-1] + 1; i < a.length; i++) {
s += a[i];
// s+=a[idx[lvcurrent-1]]*a[idx[i]];
}
return s;
} else {
// System.out.println("b:"+idx[lvcurrent]);
for (int i = idx[lvcurrent-1] + 1; i < a.length; i++) {
idx[lvcurrent] = i;
s += a[i] * run97_4_1(a, idx, lvcurrent + 1, lvall);
}
return s;
}
}
在matlab上测试一下结果对比一下
夹娃:
private void run97() {
int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
System.out.println(run97_4(a, 2));
System.out.println(run97_4(a, 3));
System.out.println(run97_4(a, 4));
System.out.println(run97_4(a, 5));
System.out.println(run97_4(a, 6));
System.out.println(run97_4(a, 7));
}
private int run97_4(int[] a, int lvall) {
int[] idx = new int[lvall];
idx[0]=-1;
return run97_4_1(a, idx, 1, lvall);
}
public static void main(String[] args) {
Test1 t = new Test1();
t.run97();
}
matlab
syms x y;
y=sym((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6));
expand(y)
一致.恩,还马马虎虎
(*..*)