不定数组求和问题

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  SumFunc
{
    
public partial class Form1 : Form
    
{
        
/// <summary>
        
/// The List used to store the Testing Data
        
/// </summary>

        private List<int> intArr;
        
/// <summary>
        
/// The List used to store the ready result member
        
/// </summary>

        private List<int> pendingArr;
        
/// <summary>
        
/// The List used the store the Final Result
        
/// </summary>

        private List<string> lines;
        
/// <summary>
        
/// The Int Array used to initialize the Testing Data
        
/// </summary>

        private int[] Arr;
        
/// <summary>
        
/// The Const Sum Result
        
/// </summary>

        private const int sumResult = 59;

        
public Form1()
        
{
            InitializeComponent();

            Arr 
= new int[] 4562359226116523 };
            intArr 
= new List<int>();
            pendingArr 
= new List<int>();
            lines 
= new List<string>();
            intArr.AddRange(Arr);
        }


        
private void Btn_Sum_Click(object sender, EventArgs e)
        
{
            
if (intArr.Count > 0)
            
{
                
for (int i = 0; i < intArr.Count; i++)
                
{
                    
//Every run should clear the pendingArr so that there's no unwanted ready result member
                    pendingArr.Clear();
                    
//Select one record
                    int tmpInt = intArr[i];
                    
if (tmpInt == sumResult)
                    
{
                        listResult(tmpInt.ToString());
                        
continue;
                    }


                    
if (tmpInt > sumResult) continue;

                    List
<int> tmpArr = new List<int>();
                    tmpArr.AddRange(Arr);
                    
//Exclude the selected record
                    tmpArr.RemoveAt(i);
                    
int tmpRst = tmpInt;
                    
for (int j = 0; j < tmpArr.Count; j++)
                    
{
                        tmpRst 
+= tmpArr[j];
                        
if (tmpRst < sumResult)
                        
{
                            
//Get a good ready result member
                            pendingArr.Add(tmpArr[j]);
                            
continue;
                        }


                        
if (tmpRst > sumResult)
                        
{
                            
//bad member, cut it
                            tmpRst -= tmpArr[j];
                            
continue;
                        }


                        
if (tmpRst == sumResult)
                        
{
                            
//so, we get the right sum formular
                            
//do forget the add the right member into the pendingArr
                            pendingArr.Add(tmpArr[j]);
                            
string aRst = tmpInt.ToString() + "+";
                            
for (int k = 0; k < pendingArr.Count; k++)
                            
{
                                aRst 
+= pendingArr[k].ToString();
                                
if (k != pendingArr.Count - 1)
                                
{
                                    aRst 
+= "+";
                                }

                            }

                            listResult(aRst);
                        }

                    }

                }

            }

        }

        
/// <summary>
        
/// Handle the Result
        
/// </summary>
        
/// <param name="result">The Result Formular</param>

        private void listResult(string result)
        
{
            result 
= sortResult(result);

            
if (!lines.Contains(result))
            
{
                
//A RichTextBox to show the Result
                richTextBox1.Text += result + " ";
                lines.Add(result);
            }

        }

        
/// <summary>
        
/// Sort the result in Ascending
        
/// </summary>
        
/// <param name="result">The Result Formular</param>
        
/// <returns>The Result in Ascending</returns>

        private string sortResult(string result)
        
{
            
string rst = string.Empty;

            List
<int> list = new List<int>();
            
string[] pieces = result.Split('+');
            
if (pieces.Length > 0)
            
{
                
foreach (string piece in pieces)
                
{
                    list.Add(
int.Parse(piece));
                }

            }

            
if (list.Count > 0)
            
{
                
//Using the List<T> sort the result in Ascending
                list.Sort();
                
                
//Then, output the result in formular mode
                for (int i = 0; i < list.Count; i++)
                
{
                    rst 
+= list[i].ToString();
                    
if (i != list.Count - 1)
                    
{
                        rst 
+= " + ";
                    }

                    
else
                    
{
                        rst 
+= " = " + sumResult.ToString();
                    }

                }

            }


            
return rst;
        }

    }

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值