将两个程序合并到一起

qq_41066178 2017-11-29 04:39:26
是子集和问题,但要求是电脑自动生成随机的数组,而不是自己输入


第一部分程序为生成随机数组,第二部分程序为子集和求解。
(想让生成的随机数组a[100])代替子集和求解中的num数组)
这样可以实现输入数n后,再输入想要的和(子集和问题中想要的的元素和sum),
便可以输出所有的结果,而不用手动输入num数组中的每一个元素
第一部分:
#include
#include
#include
int main(void)
{
int a[100]={0};int i,m,n;
srand((unsigned)time(NULL));
for(i=1;i<=99;i++)
{
while(a[m=rand()%100+1]);
a[m]=i;
}
scanf("%d",&n);
for(i=1;i<=n;i++)
{printf("%4d",a[i]);}

}


第二部分:
#include
#include
#define MAX 1000
//global variables
int n=0;//the number of numbers
int c=0;//the sum of the subset
int num[MAX]={0};
int count=1;//the number of the element in a subset
int result[MAX]={0}; //the answer of this question
int c_sum=0;//current sum

//prototypes
void swap(int &a,int &b);
void back_subset(int i);

int main()
{
//declaration
int i=0;
printf("Please input the number of the numbers:");
scanf("%d",&n);
printf("Please input the sum:");
scanf("%d",&c);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
back_subset(1);


getch();
}

void back_subset(int i)
{
if(c_sum+num[i]==c)
{
result[count]=num[i];
for(int temp=1;temp<=count;temp++)
printf("%d ",result[temp]);
printf("\n\n\n\n----------separate line------------------\n\n\n\n");
return ;
}
if(i>n)
return ;
if(c_sum+num[i]>c)
return;

for(int j=i;j<=n;j++)
{
result[count++]=num[j];
c_sum+=num[j];
swap(num[i],num[j]);
back_subset(i+1);
swap(num[i],num[j]);
c_sum-=num[j];
count--;
}
}

void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}



...全文
1005 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_41066178 2017-11-29
  • 打赏
  • 举报
回复
谢谢帮助,另外怎么把子集和输出的结果去重
例如,2 4 6 5 7
4 2 6 5 7
或者 5 7 4 2 6
自信男孩 2017-11-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 1000

//global variables
int n=0;//the number of numbers
int c=0;//the sum of the subset
int num[MAX]={0};
int count=1;//the number of the element in a subset
int result[MAX]={0}; //the answer of this question
int c_sum=0;//current sum

//prototypes
void swap(int &a,int &b);
void back_subset(int i);
void get_random(int *a, int len);

int main()
{
    //declaration
    //int i=0;
    printf("Please input the number of the numbers:");
    scanf("%d",&n);
    printf("Please input the sum:");
    scanf("%d",&c);
    get_random(num, n);
    /*
       for(i=1;i<=n;i++)
       scanf("%d",&num[i]);
       */
    back_subset(1);


    //getch();
}

void get_random(int *a, int len)
{
    int i;

    srand((unsigned)time(NULL));
    for(i=0;i<len;i++)
        a[i] = rand() % 100;
}

void back_subset(int i)
{
    if(c_sum+num[i]==c)
    {
        result[count]=num[i];
        for(int temp=1;temp<=count;temp++)
            printf("%d ",result[temp]);
        printf("\n\n\n\n----------separate line------------------\n\n\n\n");
        return ;
    }
    if(i>n)
        return ;
    if(c_sum+num[i]>c)
        return;

    for(int j=i;j<=n;j++)
    {
        result[count++]=num[j];
        c_sum+=num[j];
        swap(num[i],num[j]);
        back_subset(i+1);
        swap(num[i],num[j]);
        c_sum-=num[j];
        count--;
    }
}

void swap(int &a,int &b)
{
    int temp=a;
    a=b;
    b=temp;
}
参考一下吧
蓝洞 2017-11-29
  • 打赏
  • 举报
回复
用函数来表示第一个程序,第二个程序调用函数就好
老马何以识途 2017-11-29
  • 打赏
  • 举报
回复
把第一部分程序变成函数,传入缓冲区,生成随机数后填入缓冲区即可。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧