首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求个计算有关问题的算法

2013-04-05 
求个计算问题的算法static class Program{[STAThread]static void Main(){Ar(new int[] { 5, 100, 195, 10

求个计算问题的算法


    static class Program
    {
        [STAThread]
        static void Main()
        {
            Ar(new int[] { 5, 100, 195, 10 }, 200);
        }
        //先列出递归原理
        //static void Ar(int[] ar, int g)
        //{
        //    int size = ar.Length;
        //    for (int x = 0; x < size; x++)
        //    {
        //        for (int y = x + 1; y < size; y++)
        //        {
        //            for (int z = y + 1; z < size; z++)
        //            {
        //                if (x + y + z == g)
        //                    return true;
        //                else if (x + y + z > g)//结束
        //                    break;
        //                //for (int a = z + 1; a < size; a++)
        //                //...
        //            }


        //        }
        //    }
        //}

        //开始点
        static void Ar(int[] array, int target)
        {
            //结果
            int[] result = new int[array.Length];
            if (Ar(array, target, ref result, 0, 0, 0))
            {
                //成功
                Console.Write("成功:");
                for (int i = 0; i < result.Length; i++)
                {
                    if (i > 0)
                        Console.Write("+");
                    Console.Write(result[i]);
                }
                Console.Write("=" + target);
                Console.WriteLine();
                Console.Read();
            }
            else
            {
                result = new int[0];
                //失败
                Console.Write("失败");
                Console.Read();
            }
        }
        static bool Ar(int[] array, int target, ref int[] result, int index, int depth, int aggregate)
        {
            int size = array.Length;
            int val;
            for (int i = index; i < size; i++)
            {
                val = array[i] + aggregate;


                if (val > target)
                    break;
                else if (val == target)
                {
                    result[depth] = array[i];
                    Array.Resize(ref result, depth + 1);
                    return true;
                }
                result[depth] = array[i];
                if (Ar(array, target, ref result, i + 1, depth + 1, val))
                    return true;
            }
            return false;
        }
    }


求个计算有关问题的算法
[解决办法]
我这个方法可以实现你的要求:
没有做异常处理。
Console.WriteLine("请输入总数:");
int total = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入数值的个数:");
int a = Convert.ToInt32(Console.ReadLine());
int[] shuZ = new int[a];
for (int i = 0; i < shuZ.Length; i++) //把用户输入的数存到数组中;
{
Console.WriteLine("请输入第{0}个数:",i+1);
int b = Convert.ToInt32(Console.ReadLine());
shuZ[i] = b;
}
int sum = 0;
for (int i = 0; i < shuZ.Length; i++)//求输入所有数值之和;
{
sum += shuZ[i];
}
if (total != sum)
{
Console.WriteLine("0");
}
else
{
for (int i = 0; i < shuZ.Length; i++)
{
Console.Write("{0}\t",shuZ [i]);
}
}
Console.ReadKey();
楼主,看看是不是你想要的结果?

热点排行