数据拆分
例如:5 拆分为
5 = 4 + 1
5 = 3 + 1 + 1
5 = 2 + 1 + 1 + 1
5 = 1 + 1 + 1 + 1 + 1
5 = 2 + 3
//====================================================================
C#代码
/// <summary>
/// 数字拆分
/// </summary>
/// <param name="num">要拆分的数字</param>
/// <returns></returns>
private string SumNum(int num)
{
string str = "";
if (num <= 1 || num >= int.MaxValue)
{
//确定有效范围数字不能小于1
//不能大于int最大值(int最大值 int.MaxValue = 2147483647)
return str;
}
int prv = num - 1;
while (prv > 0)
{
int idx = prv;
str += num + " = " + prv + " + ";
while (idx < num)
{
if (idx < num - 1)
{
str += "1 + ";
}
else
{
str += "1";
}
idx++;
}
str += "\r\n";
prv--;
}
prv = 2;
while (prv <= num / 2)
{
str += num + " = " + prv + " + ";
int sum = 0;
for (int i = prv; i < num; i++)
{
sum += 1;
}
str += sum + "\r\n";
prv++;
}
return str;
}