++level惹的祸,我差点哭了

用递归算法从1到9(可以重复)选出9个数相加为45,并将其打印出来不能用循环的方法

 

 

using System;
using System.Collections.Generic;
using System.Text;
using CustomLib;

 

namespace ConTest
{
    class TestMain
    {
        public static void Main(string[]args)
        {
            int[] mems = new int[COUNTMEMBERS];
            RegResult(mems, level);
            Console.Read();
        }
        private static  int COUNTMEMBERS = 9;
        private static  int MINVALUE = 1;
        private static  int MAXVALUE = 9;
        private static  int VALUE = 45;
        private static int level = 0;      


        private static void RegResult(int[]mems,int level)
        {

            for (int i = MINVALUE; i <= MAXVALUE; i++)
            {
                mems[level] = i;

                 if (level == COUNTMEMBERS - 1)
                {

                    if (IsEqualValue(mems))                    
                        DisplayDatas(mems);  

                }
                else

                    //RegResult(mems,++level );//注意这里,哥就因为这设置为++level,几乎耗了我半天 ,我不断的怀疑我的逻辑,找来找去,真蛋疼,就是找不出原因,现终于水落石出,一身松。                                  
                    RegResult(mems, level+1);
                
            }
              
 
           
        }

 

//验证是否符合条件

        private static bool IsEqualValue(int[] mems)
        {

            int sum = 0;
            for (int i = 0; i < mems.Length;i++ )
            {
                sum += mems[i];
            }
            return sum == VALUE;
        }

//将符合条件的数组打印出来

        private static void DisplayDatas(int[]datas)
        {

          
          
                Console.Write("Datas:\t");

                foreach (int i in datas)
                {

                    Console.Write(i + " ");
                }
                Console.WriteLine("Total:" + VALUE.ToString());
         
           
        }
       
    }
}

 

 

优化代码:

using System;
using System.Collections.Generic;
using System.Text;
using CustomLib;

 

namespace ConTest
{
    class TestMain
    {
         public static void Main(string[]args)
        {
            int total = 0;
            int level = 0;
            int[] mems = new int[COUNTMEMBERS];
            RegResult(mems, level,total);           
            Console.Read();
        }
        private static  int COUNTMEMBERS = 9;
        private static  int MINVALUE = 1;
        private static  int MAXVALUE = 9;
        private static  int VALUE = 72;
        private static int level = 0; 


        private static void RegResult(int[]mems,int level,int total)
        {

            for (int i = MINVALUE; i <= MAXVALUE; i++)
            {
                mems[level] = i;
                total = total + i;

                 if (level == COUNTMEMBERS - 1)
                {

                    if (total == VALUE)
                        DisplayDatas(mems, total);

                }
                else
                {
                    
                    RegResult(mems, level+1,total);
                }

                 total = total - i;//这句必须要加上
            }
              
 
           
        }

        private static bool IsEqualValue(int[] mems)
        {

            int sum = 0;
            for (int i = 0; i < mems.Length;i++ )
            {
                sum += mems[i];
            }
            return sum == VALUE;
        }

        private static void DisplayDatas(int[]datas,int total)
        {       
           
                Console.Write("Datas:\t");

                foreach (int i in datas)
                {

                    Console.Write(i + " ");
                }
                Console.WriteLine("Total:" + total.ToString());         
            
        }
       
    }
}

 

 

//循环算法与递归算法的比较

    private static  int COUNTMEMBERS = 9;
        private static  int MINVALUE = 1;
        private static  int MAXVALUE = 9;
        private static  int VALUE = 45;
        private static int level = 0;
        private static int[] mems = new int[COUNTMEMBERS];
     //循环算法的设计,虽然下面的循环代码也能实现同样的功能,但是随着层数的增多,这些代码就会臭名昭著,一滩死水,我看了都呕吐
        private static void LoopResult()
        {
            for (int i = MINVALUE; i <= MAXVALUE; i++)
            {
                mems[0] = i;
                for (int j = MINVALUE; j <= MAXVALUE; j++)
                {
                    mems[1] = j;
                    for (int k = MINVALUE; k <= MAXVALUE; k++)
                    {
                        mems[2] = k;
                        for (int l = MINVALUE; l <= MAXVALUE; l++)
                        {
                            mems[3] = l;
                            for (int m = MINVALUE; m <= MAXVALUE; m++)
                            {
                                mems[4] = m;
                                for (int n = MINVALUE; n <= MAXVALUE; n++)
                                {
                                    mems[5] = n;
                                    for (int o = MINVALUE; o <= MAXVALUE; o++)
                                    {
                                        mems[6] = o;
                                        for (int p = MINVALUE; p <= MAXVALUE; p++)
                                        {
                                            mems[7] = p;
                                            for (int q = MINVALUE; q <= MAXVALUE; q++)
                                            {
                                                mems[8] = q;
                                                int sum = 0;
                                                foreach (int item in mems)
                                                {
                                                    sum += item;
                                                }
                                                if (sum == VALUE)
                                                {
                                                    for (int a = 0; a < mems.Length; a++)
                                                    {
                                                        Console.Write(mems[a] + "\t");
                                                    }
                                                }
                                               
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

           
           
        }
        //递归循环的设计,多优雅,多简洁,值得推荐

        private static void RegResult(int[]mems,int level,int total)
        {

            for (int i = MINVALUE; i <= MAXVALUE; i++)
            {
                mems[level] = i;
                total = total + i;

                 if (level == COUNTMEMBERS - 1)
                {

                    if (total == VALUE)
                        DisplayDatas(mems, total);                }
                else                                    
                    RegResult(mems, level+1,total);
               

                 total = total - i;
            }    
         }

//将符合条件的打印出来

 private static void DisplayDatas(int[]datas,int total)
        {

                Console.Write("Datas:\t");

                foreach (int i in datas)
                {

                    Console.Write(i + " ");
                }
                Console.WriteLine("Total:" + total.ToString());          
            
        }

 

  public static void Main(string[]args)
        {
            int total = 0;
           // int level = 0;
           //递归算法的调用
            RegResult(mems, level,total);
            //循环算法的调用
            LoopResult();
          
            Console.Read();
        }

posted @ 2013-03-27 15:25  Predator  阅读(130)  评论(0编辑  收藏  举报