编写电源集代码时出现问题
Problems with writing powerset 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";
}
}
好的,我无法调试您丢失的代码。但自从你发布了你的问题后,我用纯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<N;j++){
if((i & (1<<j))){
printf("%c ",y[j]);
}
}
printf("n");
}
printf("n");
return EXIT_SUCCESS;
}
它的工作原理:
:~$ gcc x.c -lm -Wall
:~$ ./a.out
a
b
a b
c
a c
b c
a b c
[答案]
错误情况:当两个符号相同时。
y[0] = 'a';
y[1] = 'a';
y[2] = 'c';
:~$ ./a.out
a
a
a a
c
a c
a c
a a c
但根据集合论,这是错误的。因为集合中的不能有两次相同的元素(多于一个)。但是INPUT也是错误的:(a, a, c)
不是一个集合。所以代码是可用的。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在java中解决这段代码时面临循环中的问题
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 读取最后一行代码算法 - c++ 时出现问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- Xcode 语义问题引用或以前定义的代码
- 需要以下代码的帮助,下面的代码有什么问题
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 如何在代码问题中解决此问题 代码强制
- C 代码问题?编译器问题?代码块
- C - 如果语句问题.代码将不会运行其他,否则
- 简单的NOOB C 输入/输出问题:代码错误
- NDK:0xdeadd00d时虚拟机中止和致命信号 11 (SIGSEGV) 的问题(代码 = 1)
- 编译问题 - 代码 C2129
- c++问题代码段
- 模板作用域问题代码无法编译
- 基本 CUDA 程序的问题.代码或编译器