首页 新闻 搜索 专区 学院

求简单方法(计算1-9中的数加起来等于输入的数的方法有几种(如:输入5:就有1+4,2+3不能1+2+2等))

0
悬赏园豆:10 [待解决问题]

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

namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }

        string lastStr;         string xx;         int num;         int count;

        private void button1_Click(object sender, EventArgs e)         {             count = 0;             this.label1.Text = "";             textBox2.Clear();             num = Convert.ToInt32(textBox1.Text.Trim());             run();             label1.Text = "种共有【" + count.ToString() + "】种算法";         }         void run()         {             lastStr = "";             for (int i1 = 0; i1 <= 1; i1++)                 for (int i2 = 0; i2 <= 1; i2++)                     for (int i3 = 0; i3 <= 1; i3++)                         for (int i4 = 0; i4 <= 1; i4++)                             for (int i5 = 0; i5 <= 1; i5++)                                 for (int i6 = 0; i6 <= 1; i6++)                                     for (int i7 = 0; i7 <= 1; i7++)                                         for (int i8 = 0; i8 <= 1; i8++)                                             for (int i9 = 0; i9 <= 1; i9++)                                             {                                                 lastStr = i1.ToString() + i2.ToString() + i3.ToString() + i4.ToString() + i5.ToString() + i6.ToString() + i7.ToString() + i8.ToString() + i9.ToString();                                                 zhuanhua();                                             }         }

        public void zhuanhua()         {             string starstr = "123456789";             for (int ii = 0; ii < 9; ii++)             {                 string x = lastStr.Substring(ii, 1);                 string y = starstr.Substring(ii, 1);                 if (x == "1")                 {                     xx = xx + y;                 }             }             if (xx == null)                 return;             panduan();         }

        public void panduan() {             int tt = 0;             string pp = "";             for (int i = 0; i < xx.Length; i++)             {                 string x = xx.Substring(i, 1);                 pp = x + "+" + pp;                 int xxx = Convert.ToInt32(x);                 tt = tt + xxx;             }             if (tt == num)             {                 pp = pp.Substring(0, pp.Length - 1);                 textBox2.AppendText(pp + "=" + tt + "  ");                 count++;             }             else             {                 xx = "";             }           }          } }

枫林海的主页 枫林海 | 菜鸟二级 | 园豆:204
提问于:2012-03-30 11:07
< >
分享
所有回答(3)
-1

能否先把1-9的数加起来所有情况都列出来,然后看结果是5的有多少

死白的man | 园豆:2135 (老鸟四级) | 2012-03-30 11:21

可以上面的代码、可以实现功能、你可以看看、在把你的想法进行实现。应该就行了 。谢。。

支持(0) 反对(0) 枫林海 | 园豆:204 (菜鸟二级) | 2012-03-30 11:26

@枫林海: 嘿嘿点错了

支持(0) 反对(0) 枫林海 | 园豆:204 (菜鸟二级) | 2012-03-30 11:48
1

已经有人问过了:http://q.cnblogs.com/q/33111/

水牛刀刀 | 园豆:6350 (大侠五级) | 2012-03-30 11:37

谢谢你的提醒、我会去看的。不过那个好像是(A=A+B的模式、我要的是A=b+c、A=B+c+d、A=B+c+d+e、A=B+c+d+e+...的都可以)

支持(0) 反对(0) 枫林海 | 园豆:204 (菜鸟二级) | 2012-03-30 11:47

@枫林海: 看清楚,都可以的。其中有个答案是我回答的,我能不知道么。。。

支持(0) 反对(0) 水牛刀刀 | 园豆:6350 (大侠五级) | 2012-03-30 13:07
1

递归:http://jsfiddle.net/FnENA/

var s = "";
function getSumMethods(n,prev)
{
if (n<=0)
{
s += prev.join("+") + "\n";
return prev;
}
var i;
for(i=1;i<=n;++i)
{
var cloned = prev.slice();
cloned.push(i);
getMethods(n-i,cloned);
}
}

getSumMethods(9,[]);
alert(s)
Greatest | 园豆:678 (小虾三级) | 2012-03-31 16:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册