求和为指定数字的所有组合

(July——微软等面试100题 21和25http://topic.csdn.net/u/20101023/20/5652ccd7-d510-4c10-9671-307a56006e6d.html

21.输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来.

 

解法:采用0-1背包的思想,使用递归方法:

  当选择n时,就用剩下的n-1填满 m-n;

  当不选择n是,就用剩下的n-1填满m;

 注意的是,当m=n时,即找到了符合条件的解。

 1 #include<iostream>
 2 #include<list>
 3 using namespace std;
 4 
 5 list<int> list1;
 6 void find(int sum, int n)
 7 {
 8     //递归出口
 9     if(sum <= 0 || n <= 0)
10         return;
11 
12     //输出找到的结果
13     if(sum == n)    //表示找到了一个值
14     {
15         list1.reverse();    //使输出顺序更规范
16         for(list<int>::iterator i = list1.begin(); i != list1.end(); i++)
17             cout << *i <<" ";
18         cout << n << endl;
19         list1.reverse();
20     }
21 
22     list1.push_front(n);
23     find(sum-n, n-1);    //如果放入n,则从剩余n-1个数中填满sum-n
24     list1.pop_front();
25     find(sum, n-1);        //如果不放入n,从n-1个数中填满sum
26 }
27 
28 int main()
29 {
30     int sum, n;
31     sum = 6;
32     n = 8;
33     find(sum, n);
34 
35     system("pause");
36     return 0;
37 }

 25.写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 #define MAX 100
 7 
 8 char str[MAX+1];
 9 char outstr[MAX+1];
10 
11 int continuemax(char *str, char *outstr)
12 {
13     int length = strlen(str);
14 
15     char temp[MAX];
16     int index = 0, maxl = 0;
17     for(int i = 0; i < length; i++)
18     {
19         if(str[i] >= '0' && str[i] <= '9')
20             temp[index++] = str[i];
21         else
22         {
23             if(maxl < index)
24             {
25                 temp[index] = '\0';
26                 maxl = index;
27                 strncpy(outstr, temp, MAX);
28             }
29             index = 0;
30         }
31     }
32     if(maxl < index)    //防止出现数字在最后的情况
33     {
34         temp[index] = '\0';
35         maxl = index;
36         strncpy(outstr, temp, MAX);
37     }
38     return maxl;
39 }
40 
41 int main()
42 {
43     strncpy(str, "abcd12345ed125ss123456789", MAX);        //strncpy只有在源字符串短于100是才会在 str最后加上\0,当被截断时,不保证后面有null字符
44     str[MAX] = '\0';
45 
46     int t = continuemax(str, outstr);
47 
48     printf("%d length, number is %s.\n", t, outstr);
49 
50     system("pause");
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/dandingyy/archive/2012/09/26/2704728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要明确问题的具体要求。假设我们已经有一个包含多个数字的Excel表格,并且我们想要通过VBA编程的方式,找出这些数字中的若干个数字,使其相加的和等于一个固定的数。 下面是一种解决方案的例子: 1. 首先,打开Excel表格并按下"ALT + F11"打开VBA编辑器。 2. 在VBA编辑器中,选择"插入" -> "模块",以在工作簿中添加一个新的模块。 3. 在新模块中编写以下代码: ```vba Sub FindSum() Dim rng As Range Dim sumValue As Double Dim cell As Range Dim total As Double ' 设置你想要的固定数 sumValue = 100 ' 设置你表格的范围 Set rng = Range("A1:A10") ' 开始计算 For Each cell In rng total = cell.Value ' 使用嵌套循环来找出所有可能的组合 For Each cell2 In rng If Not cell2 Is cell Then total = total + cell2.Value If total = sumValue Then ' 找到了符合条件的组合 MsgBox "找到了一个符合条件的组合!" Exit Sub End If End If Next cell2 Next cell ' 如果没有找到符合条件的组合 MsgBox "没有找到符合条件的组合!" End Sub ``` 4. 修改上述代码中的"rng"变量和"sumValue"变量,以适应你的实际情况。"rng"变量指定要查找的数字范围,"sumValue"变量指定要寻找的和的固定数。 5. 最后,按下"F5"键运行代码。 此代码将遍历指定的范围,并计算所有可能的组合。如果找到一个组合的总和等于固定数,则弹出一个消息框进行提醒。如果没有找到符合条件的组合,则将弹出另一个消息框。 请注意,这只是一种解决方案的例子,并且只适用于固定的范围和固定的数。根据需要,您可能需要对代码进行修改以适应您的具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值