编写poweret代码的问题 [英] Problems with writing powerset code

查看:273
本文介绍了编写poweret代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成一个集的powerset,我写了这个代码。问题是,当用户输入两个类似的成员,它不能正常工作。我能做什么?
这是我的代码:

  #include< iostream> 
#include< string.h>
#include< stdio.h>
#include< stdlib.h>
#include< math.h>
using namespace std;

char获取(char * p,int n)
{
int i;
for(i = 0; i {
cout<<enter member<<(i + 1)< ;
cin>> *(p + i);
}
return * p;
}

void set_display(char * p,int n)
{
cout<
for(int i = 0; i {
cout <*(p + i)<
}
cout<<};
}

void powset(char * p,int n)
{
unsigned int m =(double)pow((double)2,n)
int i,j;
for(i = 0; i {
cout<
for(j = 0; j {
if(i&(1≤j))
cout < j)。
}
cout<<} \\\
;
}
}


解决方案

我无法调试您缺少的代码。但是因为你发布了你的问题我写了一个代码在 C 。可能你觉得有帮助。

 #include< stdio.h> 
#include< stdlib.h>
#include< string.h>
#include< math.h>
int main(int argc,char * argv []){
int N;
char y [20] = {0};
int i,j;
y [0] ='a';
y [1] ='b';
y [2] ='c';
N = 3;
int powerSet;
powerSet = pow(2,N);
for(i = 0; i <(powerSet); i ++){
for(j = 0; j if((i& ; j))){
printf(%c,y [j]);
}
}
printf(\\\
);
}
printf(\\\
);
return EXIT_SUCCESS;
}

其工作原理:

 :〜$ gcc xc -lm -Wall 
:〜$ ./a.out

a
b
ab
c
ac
bc
abc





您的错误大小:两个符号相同时。

  y [0] ='a'; 
y [1] ='a';
y [2] ='c';

:〜$ ./a.out

a
a
aa
c
ac
ac
aac

但是根据集合理论,这是错误的。因为在集合中,我们不能有两次相同的元素(超过onec)。但也输入错误:(a,a,c)不是一个集合。所以代码是可用的。


I'm trying to generate the powerset of a set and I wrote this code. The problem is, when user enter two similar member of the set it dosen't work properly. What can I do? Here is my code:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

char obtain(char *p,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        cout<<"enter member"<<(i+1)<<"\n";
        cin>>*(p+i);
    }
    return *p;
}

void set_display(char *p,int n)
{
    cout<<"{";
    for(int i=0;i<n;i++)
    {
        cout<<*(p+i)<<",";
    }
    cout<<"}";
}

void powset(char *p,int n)
{
    unsigned int m = (double)pow((double)2, n);
    int i, j;
    for(i = 0; i < m; i++)
    {
        cout<<"{";
        for(j = 0; j < n; j++)
        {
            if(i& (1<<j))
                cout<<*(p+j);
        }
        cout<<"}\n";
    }
}

解决方案

Ok I could not debug your code where you are missing. but since you posted your question I written an code in pure C. May be you find it helpful.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(int argc, char* argv[]){
    int N;
    char y[20]={0};
    int i,j;
    y[0] = 'a';
    y[1] = 'b';
    y[2] = 'c'; 
    N = 3;
    int powerSet;
    powerSet=pow(2,N);
    for(i = 0; i<(powerSet);i++){
        for(j=0;j<N;j++){
            if((i & (1<<j))){
                printf("%c ",y[j]);
            }
        }
        printf("\n");
    }           
    printf("\n");
    return EXIT_SUCCESS;
}

And its working:

:~$ gcc x.c -lm -Wall
:~$ ./a.out 

a 
b 
a b 
c 
a c 
b c 
a b c 

[ANSWER]

You error case: When two symbols are same.

y[0] = 'a';
y[1] = 'a';
y[2] = 'c'; 

:~$ ./a.out 

a 
a 
a a 
c 
a c 
a c 
a a c 

But this is wrong according to set theory. Because in Set we can't have same element twice (more then onec). BUT ALSO INPUT IS WRONG: (a, a, c) is not a set. So code is usable.

这篇关于编写poweret代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆