using System;namespace ConsoleApplication2
{
/// <summary>
/// Class1 µÄժҪ˵Ã÷¡£
/// </summary>
class Class1
{
/// <summary>
/// Ó¦ÓóÌÐòµÄÖ÷Èë¿Úµã¡£
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: ÔÚ´Ë´¦Ìí¼Ó´úÂëÒÔÆô¶¯Ó¦ÓóÌÐò
//
int [] arr=new int[10]{0,1,2,3,4,5,6,7,8,9};
foreach(int a in arr)
{
foreach(int b in arr)
{
if(a+b==10)Console.WriteLine(a+"+"+b+"=10");
}
}
Console.ReadLine();
}
}
}
{
/// <summary>
/// Class1 µÄժҪ˵Ã÷¡£
/// </summary>
class Class1
{
/// <summary>
/// Ó¦ÓóÌÐòµÄÖ÷Èë¿Úµã¡£
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: ÔÚ´Ë´¦Ìí¼Ó´úÂëÒÔÆô¶¯Ó¦ÓóÌÐò
//
int [] arr=new int[10]{0,1,2,3,4,5,6,7,8,9};
foreach(int a in arr)
{
foreach(int b in arr)
{
if(a+b==10)Console.WriteLine(a+"+"+b+"=10");
}
}
Console.ReadLine();
}
}
}
foreach(int a in arr)
{
foreach(int b in arr)
{
if(a+b==10)Console.WriteLine(a+"+"+b+"=10");
}
}
Console.ReadLine();
foreach(int a in arr)
{
foreach(int b in arr)
{
foreach(int c in arr)
{
foreach(int d in arr)
{
if(a+b+c+d==10&&a!=b&&b!=c&&c!=d)Console.WriteLine(a+"+"+b+"+"+c+"+"+d+"=10");
}
if(a+b+c==10&&a!=b&&b!=c)Console.WriteLine(a+"+"+b+"+"+c+"=10");
}
if(a+b==10&&a!=b)Console.WriteLine(a+"+"+b+"=10");
}
}
1:将这些数字从小到大排序,按顺序累加求出所需的最大位数的组合;
如此例:1+2+3+4>=10 ,即可得到最多是4个数字相加,可以得到10,最少为两个数字相加得到10
2:根据此依次求出2个数累加=10的组合,3个数=10,4个数=10.....根据以上思路大概写算法如下
1到10个数
for (i=1;i<=n;i++)
2到4个数的和的组合
for (s=2;s<=4;k++)
sum(sums,a[i],2,i,s)
//递归函数:如求2位数时逐个相加即可,3为数时相加后要递归一次,4为数要递归两次,
//即递归次数等于s-2
//sums记录字符串,val已经加过的和,count是加了几位数,
j是加到数组的第几个,kinds是求几位数的和
private void sum(ref string sums,int val,ref int count,int j,int kinds )
{
int i;
if (kinds==count)
{
for (i=j;i<n;i++)
if ((val+a[i])==10)
sums=sums+i.ToString();
}
else
{
for (i=j;i<n;i++)
if (val+a[i]<10)
{
count=count+1;
sum(ref sums,val+a[i],count,i,kinds);
}
}}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace NumSum
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
string strSTime=DateTime.Now.ToString();
int[] array=new int[20]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};//原始数组
int[] flag=new int[20]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //标志数组,表示哪个数字被选中(1,表示被选中.0,表示未被选中)
int sum=25; //和 string strMsg=""; for(int i=0;i<array.Length-1;i++)
{
for(int j=i+1;j<array.Length;j++)
{
flag=new int[20]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
flag[i]=1; plus(ref strMsg,array[i],j,array,flag,sum); //函数返回值
}
} string strETime=DateTime.Now.ToString();
MessageBox.Show(strSTime+"||"+strETime);
MessageBox.Show(strMsg); }
/// <summary>
/// 递归算法
/// </summary>
/// <param name="strMsg">输出结果</param>
/// <param name="iCurSum">当前计算的和</param>
/// <param name="iSecIndex">相加数的索引</param>
/// <param name="array">数值数组</param>
/// <param name="flag">标志数组</param>
/// <param name="sum">和</param>
private void plus(ref string strMsg,int iCurSum,int iSecIndex,int[] array,int[] flag,int sum)
{
iCurSum=iCurSum+array[iSecIndex];//当前和
if(iCurSum<sum)
{
flag[iSecIndex]=1;
if(++iSecIndex<array.Length)
{
int k=iSecIndex;
for(;k<array.Length;k++)
{
if(iCurSum+array[k]==sum)
{
flag[k]=1;
strMsg+=plusResult(array,flag); //得到一个成功组合
flag[k]=0;
break;
}
}
if(k!=iSecIndex)
{
plus(ref strMsg,iCurSum,iSecIndex,array,flag,sum); //递归调用
}
}
}
else if(iCurSum==sum)
{
flag[iSecIndex]=1;
strMsg+=plusResult(array,flag); //得到一个成功组合
flag[iSecIndex]=0;
}
else if(iCurSum>sum)
{
flag[iSecIndex]=0;
}
}
/// <summary>
/// 输出一个满足条件的组合
/// </summary>
/// <param name="array"></param>
/// <param name="flag"></param>
/// <returns></returns>
private string plusResult(int[] array,int[] flag)
{
string strMsg="";
for(int m=0;m<flag.Length;m++)
{
if(flag[m]==1)
{
strMsg+=array[m].ToString()+",";
}
}
return strMsg+"||";
}
}
}