# 把N层for循环怎么转换成递归形式

0

`public Hashtable GetHashtable()        {         //数组不确定,有可能加有可能减 现在这种做法是固定死的嵌套循环           //想改成递归做法,可以根据动态的数组长度进行循环            int[] arr = new int[] {1,2, 4, 8, 16, 32, 64 };            Hashtable dic = new Hashtable();            for (int i = 0; i < arr.Length; i++)            {                string str = string.Empty;                int value = 0;                for (int j = i + 1; j < arr.Length; j++)                {                    str = arr[i].ToString() + "+" + arr[j].ToString();                    value = arr[i] + arr[j];                    dic.Add(str, value);                    for (int k = j + 1; k < arr.Length; k++)                    {                        str = arr[i].ToString() + "+" + arr[j].ToString() + "+" + arr[k].ToString();                        value = arr[i] + arr[j] + arr[k];                        dic.Add(str, value);                        for (int n = k + 1; n < arr.Length; n++)                        {                            str = arr[i].ToString() + "+" + arr[j].ToString() + "+" + arr[k].ToString() + "+" + arr[n].ToString();                            value = arr[i] + arr[j] + arr[k] + arr[n];                            dic.Add(str, value);                            for (int m = n + 1; m < arr.Length; m++)                            {                                str = arr[i].ToString() + "+" + arr[j].ToString() + "+" + arr[k].ToString() + "+" + arr[n].ToString() + "+" + arr[m].ToString();                                value = arr[i] + arr[j] + arr[k] + arr[n] + arr[m];                                dic.Add(str, value);                                for (int a = m + 1; a < arr.Length; a++)                                {                                    str = arr[i].ToString() + "+" + arr[j].ToString() + "+" + arr[k].ToString() + "+" + arr[n].ToString() + "+" + arr[m].ToString() + "+" + arr[a].ToString();                                    value = arr[i] + arr[j] + arr[k] + arr[n] + arr[m] + arr[a];                                    dic.Add(str, value);                                    for (int b = a + 1; b < arr.Length; b++)                                    {                                        str = arr[i].ToString() + "+" + arr[j].ToString() + "+" + arr[k].ToString() + "+" + arr[n].ToString() + "+" + arr[m].ToString() + "+" + arr[a].ToString() + "+" + arr[b].ToString();                                        value = arr[i] + arr[j] + arr[k] + arr[n] + arr[m] + arr[a] + arr[b];                                        dic.Add(str, value);                                    }                                }                            }                        }                    }                }            }            return dic;        }`

0

`            int[] arr = new int[] {1,2, 4, 8, 16, 32, 64 };            Hashtable dic = new Hashtable();            int max = arr[arr.Length - 1] * 2 - 1; //最大值是所有位都是1的情况            for (int data = 1; data <= max; data++)            {                int bit = 0x00000001;                int hitCount = 0; //某一位为1的次数，在这里，我们要排除掉只有一个数的情况                string key = "";                for (int i = 0; i < arr.Length; i++) //最多遍历到数组指定的最大位                {                    if ((data & bit) != 0)                    {                        if (hitCount++ == 0)                        {                            key = Math.Pow(2, i).ToString();                        }                        else                        {                            key += "+" + Math.Pow(2, i).ToString();                        }                    }                    bit <<= 1;                }                if (hitCount > 1)                {                    dic.Add(key, data);                }            }`

eaglet | 专家六级 |园豆：17139 | 2010-05-28 10:37

0

`int[] arr = new int[] {1,2, 4, 8, 16, 32, 64 };Hashtable dic = new Hashtable();GetHashtable(arr,0);public Hashtable GetHashtable(int[] arr,int index ){string mstr =string.Empty;int mvalue =0;if(index==0) //第一层{for (int i = 0; i < arr.Length; i++){mstr +=arr[i].ToString();mvalue +=arr[i];GetHashtable(arr,i);}}else //随后的递归{mstr +=arr[i].ToString();mvalue +=arr[i]; dic.Add(mstr, mvalue);for (int i = index + 1; i < arr.Length; i++){string str =mstr+arr[i].ToString();int value =mvalue+arr[i];dic.Add(str, value);GetHashtable(arr,i);}}}`

LittlePeng | 园豆：3445 (老鸟四级) | 2010-05-28 10:53

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