# a+b+c+d=10，怎么找出所有可能？求教

0

0

public int GetValue()

{

int sum=0;

for(int a=0;a<11;a++)

{

for(int b=0;b<11;b++)

{

for(int c=0;c<11;c++)

{

for(int d=0;d<11;d++)

{

int num=a+b+c+d;

if(num==10)

{

sum+=1;

}

}

}

}

}

return sum;

}

sunlary | 园豆：934 (小虾三级) | 2011-12-02 15:01

1
`static Stack<int> current = new Stack<int>();static List<int[]> all = new List<int[]>();static void Main(string[] args){    int n = 4;    int number = 10;            `
`    Process(1, number, n);    `
`    foreach (var answer in all)    {        Console.WriteLine(string.Join(",", answer));    }    Console.Read();}static void Process(int start, int remainingNumber, int remainingN){    int max = remainingNumber / remainingN;    for (int i = start; i <= max; i++)    {        if (remainingN == 1)        {            current.Push(remainingNumber);            all.Add(current.Reverse().ToArray());            current.Pop();            break;         }         else         {            current.Push(i);             Process(i, remainingNumber - i, remainingN - 1);            current.Pop();         }    }}`

`        static Stack<int> current = new Stack<int>();        static List<int[]> all = new List<int[]>();        static void Main(string[] args)        {            int varCount = 4;            int startValue = 1;            int number = 10;            // 在 进入Process之间进行输入参数合法性检查，也可以在Process内部进行检查，但在这里效率更高            if ((number - varCount * startValue) < 0)            {                Console.WriteLine("输入参数错误！");                return;            }            Process(startValue, number, varCount);            foreach (var answer in all)            {                Console.WriteLine(string.Join(",", answer));            }            Console.Read();        }        static void Process(int start, int remainingNumber, int remainingN)        {            // 存在逻辑错误            // int max = remainingNumber / remainingN;            int max = remainingNumber - (remainingN - 1) * start;            for (int i = start; i <= max; i++)            {                if (remainingN == 1)                {                    current.Push(remainingNumber);                    all.Add(current.Reverse().ToArray());                    current.Pop();                    break;                }                else                {                    current.Push(i);                    Process(i, remainingNumber - i, remainingN - 1);                    current.Pop();                }            }        }`

@胡屯: 懒得写检查了。只提供大体思路而已。

@胡屯: int max = remainingNumber / remainingN; 才是对的。你再仔细想想。

@胡屯:

static Stack<int> current = new Stack<int>();
static List<int[]> all = new List<int[]>();

@夫人的老公: ....C的话，你就得自己实现一个栈了……算了我懒的写了，反正我算法提供了。

0
`//实际上，这个题目如果只是4个，那么非常的简单，直接用for。//但是循环的时候，加个限制，计算数目的速度就非常的快。#include <stdio.h>int main(){    int a, b, c, d;    int sum = 1000;    long long total = 0;    for (a = 0; a <= sum; a++)    {        for (b = 0; b <= (sum - a); b++)        {            for (c = 0; c <= (sum - a - b); c++)            {                d = sum - a - b -c;                if (d >= 0) {                    total++;                    //printf("%d %d %d %d\n", a, b, c, d);                } else {                    break;                }            }        }    }    printf("%ld\n", total);}`

您需要登录以后才能回答，未注册用户请先注册