matlab中,关于用参数控制for循环层数的问题
我想编一个程序,穷举n个人分m块钱的问题之前试着编了一个10个人分100快钱的,用了10个for的嵌套,但是人数不定的话要怎么改呢?
之前看论坛上说用for实现不了,要用递归算法的非递归实现,自己模拟栈。然后用while来做循环。不是很明白。。。:'(
另,可能的话能不能把它改成并行计算的呢(算的实在太慢了)?求各位高手帮忙~~~~
a=zeros(1,101);
i=1;
for x1=0:10;
for x2=x1:12;
m2=x1+x2;
for x3=x2:13;
m3=m2+x3;
for x4=x3:15;
m4=m3+x4;
for x5=x4:17;
m5=m4+x5;
for x6=x5:20;
m6=m5+x6;
for x7=x6:25;
m7=m6+x7;
for x8=x7:34;
m8=m7+x8;
for x9=x8:50;
m9=m8+x9;
t=100-m9;
if t>=x9;
x10=t;
a(x1+1)=a(x1+1)+1;
a(x2+1)=a(x2+1)+1;
a(x3+1)=a(x3+1)+1;
a(x4+1)=a(x4+1)+1;
a(x5+1)=a(x5+1)+1;
a(x6+1)=a(x6+1)+1;
a(x7+1)=a(x7+1)+1;
a(x8+1)=a(x8+1)+1;
a(x9+1)=a(x9+1)+1;
a(x10+1)=a(x10+1)+1;
fid = fopen('d:\test.xls','a+t');
fprintf(fid,'%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n',...
x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,gn);
fclose(fid);
i=i+1;
end
end
end
end
end
end
end
end
end
end