数组部分之和问题
来源:互联网 发布:rust知乎 编辑:程序博客网 时间:2024/06/09 16:13
数组部分之和问题
一个集合x有都不相同的n个元素,使用这个集合中的不定个数的元素,组成一个和为s的序列,求出所有符合的序列,元素可以重复使用,只要元素的个数相同不考虑顺序。
比如集合是x={2,3,4,5,7}; n=5, s=12可以得出以下的序列:
2 2 2 2 2 2
2 2 2 2 4
2 2 2 3 3
2 2 3 5
2 2 4 4
2 3 3 4
2 3 7
2 5 5
3 3 3 3
3 4 5
4 4 4
5 7
- #include <iostream>
- #include <vector>
- using namespace std;
- int PushVector(int *x,int n,int s,vector<int> &tablelist,int Position)
- {
- if(s<0)
- {
- tablelist.clear();
- return 0;
- }
- else if(s==0)
- {
- for(int i=0;i<tablelist.size();i++)
- {
- cout<<tablelist[i]<<"\t";
- }
- cout<<"\n";
- return 0;
- }
- else if(s>0)
- {
- for(int i=Position;i<n;i++)
- {
- vector<int> newtablelist;
- for (int j=0;j<tablelist.size();j++)
- {
- newtablelist.push_back(tablelist[j]);
- }
- newtablelist.push_back(x[i]);
- int news = s-x[i];
- //cout<<i<<","<<news<<"\n";
- PushVector(x,n,news,newtablelist,i);
- }
- }
- else
- {
- }
- return 0;
- }
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- int FindResult(int * x,int n,int s)
- {
- vector<int> tablelist;
- tablelist.clear();
- PushVector(x,n,s,tablelist,0);
- return 0;
- }
- int main(int argc, char** argv)
- {
- int x[]= {2,3,4,5,7};
- int n =5;
- int s=12;
- FindResult(x,n,s);
- system("pause");
- return 0;
- }
- #include <iostream>
- #include <vector>
- using namespace std;
- int PushVector(int *x,int n,int s,vector<int> &tablelist,int Position)
- {
- if(s<0)
- {
- tablelist.clear();
- return 0;
- }
- else if(s==0)
- {
- for(int i=0;i<tablelist.size();i++)
- {
- cout<<tablelist[i]<<"\t";
- }
- cout<<"\n";
- return 0;
- }
- else if(s>0)
- {
- for(int i=Position;i<n;i++)
- {
- vector<int> newtablelist;
- for (int j=0;j<tablelist.size();j++)
- {
- newtablelist.push_back(tablelist[j]);
- }
- newtablelist.push_back(x[i]);
- int news = s-x[i];
- //cout<<i<<","<<news<<"\n";
- PushVector(x,n,news,newtablelist,i);
- }
- }
- else
- {
- }
- return 0;
- }
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- int FindResult(int * x,int n,int s)
- {
- vector<int> tablelist;
- tablelist.clear();
- PushVector(x,n,s,tablelist,0);
- return 0;
- }
- int main(int argc, char** argv)
- {
- int x[]= {2,3,4,5,7};
- int n =5;
- int s=12;
- FindResult(x,n,s);
- system("pause");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- #include <memory.h>
- #define _DEBUG 1
- #define MAXM 20
- #define MAXN 10
- int arr[MAXN];//数数组
- bool path[MAXN][MAXM+1];//路线
- bool hashtable[MAXM+1];//是否存在
- int c;//记录打印队列大小
- int v[MAXM+1];//打印队列,数组大小最大为MAXN+1,此时每个数为1
- void printPath(int i,int j,int c){
- if(path[i][j] &&j-arr[i]==0){//第一个点
- printf("%d ",arr[i]);
- for(int t=c-1;t>0;t--){
- printf("%d ",v[t]);
- }
- printf("%d\n",v[0]);
- return;
- }
- assert(path[i][j]);
- v[c++]=arr[i];//将元素加入到打印队列中
- for(int k=1;k<=i;k++){
- if(path[k][j-arr[i]])
- printPath(k,j-arr[i],c);
- }
- c--;//将元素从打印队列中删除
- }
- void solve(int size,int m){//size表示数组的大小,从1开始
- int i,j;
- //初始化
- memset(path,false,sizeof(path));
- hashtable[0]=true;
- for(i=1;i<=size;i++){
- for(j=arr[i];j<=m;j++){
- if(hashtable[j-arr[i]]){
- hashtable[j]=true;
- path[i][j]=true;
- }
- }
- }
- for(i=1;i<=size;i++){
- if(path[i][m]){
- printPath(i,m,0);
- }
- }
- }
- int main(){
- #if _DEBUG==1
- freopen("interview.in","r",stdin);
- #endif
- int n,m;
- int i,j;
- scanf("%d %d",&n,&m);
- for(i=1;i<=n;i++){
- scanf("%d",&arr[i]);
- }
- solve(n,m);
- return 0;
- }
- 数组部分之和问题
- 数组部分之和问题
- 数组部分之和问题
- 两个数组之和的问题
- [算法]子数组之和问题
- 数组之和
- 《Java面试试题》找出前半部分数组和后半部分数组之和相同的索引
- LeetCode 题目之 数组两数之和问题
- 求最大子数组之和及其一些扩展问题
- 子数组之和
- 最大子数组之和
- 最大子数组之和
- 最大子数组之和
- 子数组之和
- 子数组之和
- 题目:子数组之和
- 二维数组之和
- 题目:子数组之和
- linux 环境变量设置(临时 + 永久)
- Infragistics NetAdvantage UltraGrid的使用
- 【白话经典算法系列之十二】数组中只出现1次的两个数字(百度面试题)
- 系统托盘Shell_NotifyIcon函数的简单运用
- 编程逻辑性思维的重要性
- 数组部分之和问题
- 机器视觉相关术语
- AndroidManifest.xml解析
- Python网络02 Python服务器进化
- 流程控制和字符串处理
- mini2440烧写u-boot(一)
- Css的学习:ul li 问题收集
- find函数 笔记
- itextsharp使用详解