怎么写a+b+c+d+e+f=120呀(120为可变更的值)
求出abcdef所有可行的值..
abcdef的值为1-100之间比如
a+b+c+d+e+f=10
1+2+1+1+1+4=10
2+2+2+2+1+1=10
........
不用多个for来循环
在线等.....
求出abcdef所有可行的值..
abcdef的值为1-100之间比如
a+b+c+d+e+f=10
1+2+1+1+1+4=10
2+2+2+2+1+1=10
........
不用多个for来循环
在线等.....
解决方案 »
- 如何单击某个按钮,然后某个单选框被选中,同时执行了它的单击事件?
- 问一个关于C#调用纸型的问题
- 有经验者请进:关于工作流的讨论
- C# 2005 WINFORM DataGridView 输入后,回车 如何在回车时得到输入的字符??需高手回答
- C#里面是否有宏定义啊?谢谢
- DataGrid在模板列中放入imagebutton,如何判断点击的是哪一行? 急!
- dataGrid增行问题
- 大哥们帮我看看吧
- 一個Listbox控件都搞不定啊﹐各位幫幫忙啦
- 关于编译dll问题!大家帮忙看看!
- 请问为何vs.2003 c sharp赋值语句返回的是地址而不是值
- GZipStream 流中的read 的最后的100为什么?
a+b+c+d+e+f=120
本来得出的结果就比较庞大了
{
static void Main(string[] args)
{
Combine(120, 5);
Console.ReadLine();
}
static void Combine(int sum, int numCount)
{
int[] pos = new int[numCount];
for (int i = 0; i < pos.Length; i++)
{
pos[i] = i + 1;
}
int curNum = numCount - 1;
while (pos[0] <= sum - numCount)
{
if (pos[curNum] <= sum - (numCount - curNum))
{
PrintArr(sum, pos);
pos[curNum]++;
}
else
{
curNum--;
if (curNum < 0)
{
break;
}
}
}
}
static void PrintArr(int sum, int[] pos)
{
for (int i = 0; i < pos.Length ; i++)
{ if (i == pos.Length - 1)
{
Console.Write(sum - pos[i]);
Console.WriteLine();
}
else if (i == 0)
{
Console.Write(pos[i] - 0+"+");
}
else
{
Console.Write(pos[i + 1] - pos[i] + "+");
}
}
}
}
shrinerain(圣影雨)的代码我测试了好像不行啊,甚至有些只出现5个加数的
------------------------------------------------------------------
Sorry, I didn't test it...Modify the while loop, it's OK now...
while (curNum>=0)
{
PrintArr(sum, pos);
if (pos[curNum] < sum - (numCount - curNum))
{
pos[curNum]++;
}
else
{
curNum--;
}
}
知道前5字值,第六个值肯定是唯一的,减一下就出来了
-------------------------------------
Why?
U tried it?
///////////////////////////
数学建模工具,拿名词唬人呢~
#include<iostream>
using namespace std;
#define COUNT 6
#define RESULT 120
int save[COUNT+1];
void fun(int nCount,int nStart,int nTotal)
{
if(nCount==1)
{
save[nCount]=nTotal;///
for(int i=COUNT;i>1;i--)///
cout<<save[i]<<"+";///
cout<<save[1]<<"="<<RESULT<<endl;///
return;
}
for(int i=nStart;i<=nTotal/nCount;i++)
{
save[nCount]=i;///
fun(nCount-1,i,nTotal-i);
}
}
void main()
{
fun(COUNT,1,RESULT);
}
不知mathematica的内核是怎么写的,计算非常的快
尤其大数计算
#include<iostream>
using namespace std;#define MIN(a,b) (a<b?a:b)
#define COUNT 6
#define RESULT 120
#define MIN_ 1
#define MAX_ 100
int save[COUNT+1];
int nCountResult; //统计得到结果的数目void fun(int nCount,int nMin,int nMax,int nTotal)
{
if(nCount==1)
{
if(nTotal<=nMax)
{
save[nCount]=nTotal;
for(int i=COUNT;i>1;i--)
cout<<save[i]<<"+";
cout<<save[1]<<"="<<RESULT<<endl;
nCountResult++;
} return;
}
for(int i=nMin;i<=MIN(nTotal/nCount,nMax);i++)
{
save[nCount]=i;
fun(nCount-1,i,nMax,nTotal-i);
}
}
void main()
{
fun(COUNT,MIN_,MAX_,RESULT);
cout<<"结果数目:"<<nCountResult<<endl;
}