C语言编程,题:输入M个不同的字符,从中选出N个字符,输出所有可能的方案。M,N由键盘输入.xie

 我来答
scholar165
2009-06-19 · TA获得超过487个赞
知道小有建树答主
回答量:882
采纳率:50%
帮助的人:233万
展开全部
代码如下(最多支持M=50),当然可以改

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_SET ( 50U )
#define MAX_LENGTH ( ( MAX_SET ) + ( 1U ) )

/* count how many 1 in the binary form of num */
unsigned int counterOneInBinary( unsigned long num );
/* transform decimal to other radix */
void convertDecToOtherRadix( unsigned long src, char *dest, unsigned int rad );
/* add num-strlen(src) '0's to the front of string if the lenth of the string is smaller than num */
void complementZeroBefore( char *src, unsigned long num );
/* output subset by 1's in binary from src */
void outputSubsetByBinary( char *src, char *binary );

int main()
{
unsigned int total = 0;
char set[MAX_LENGTH] = {0};
unsigned int subNum = 0;
unsigned long i = 0;
char binary[MAX_LENGTH] = {0};
unsigned int totalofone = 0;
unsigned totalPow = 0l;

printf( "Input total number of the set:" );
scanf( "%d", &total );
printf( "Input the character set:" );
scanf( "%s", set );
printf( "Input the number of subset:" );
scanf( "%d", &subNum );

if( ( total != strlen( set ) ) && ( strlen( set ) > MAX_SET ) )
{
printf( "Input error!\n" );
}
else
{
totalPow = ( unsigned long )pow( 2, total );
for( i = 0; i < totalPow; i++ )
{
totalofone = counterOneInBinary( i );

if( totalofone != subNum )
{
/* no process */
}
else
{
memset( binary, 0, sizeof( binary ) );
convertDecToOtherRadix( i, binary, 2 );
complementZeroBefore( binary, total );
outputSubsetByBinary( set, binary );
}
}
}

return system( "pause" );
}

unsigned int counterOneInBinary( unsigned long num )
{
unsigned int ret = 0;
char temp[MAX_LENGTH] = {0};
char *p = temp;

convertDecToOtherRadix( num, temp, 2 );

while( *p != '\0' )
{
if( *p == '1' )
{
ret++;
}
else
{
/* no process */
}

p++;
}

return ret;
}

void convertDecToOtherRadix( unsigned long src, char *dest, unsigned int rad )
{
unsigned long num = 0;
unsigned quotient = 0;
unsigned remainder = 0;
unsigned int i = 0;

num = src;
quotient = num / rad;
remainder = num % rad;

while( quotient != 0 )
{
if( ( remainder >= 0 ) && ( remainder < 10 ) )
{
dest[i] = '0' + remainder;
}
else if( ( remainder >= 10 ) && ( remainder < 16 ) )
{
dest[i] = '0' + remainder + 'A' - '9' - 1;
}
else
{
/* no process */
}

num = quotient;
quotient = num / rad;
remainder = num % rad;
i++;
}

if( ( remainder >= 0 ) && ( remainder < 10 ) )
{
dest[i] = '0' + remainder;
}
else if( ( remainder >= 10 ) && ( remainder < 16 ) )
{
dest[i] = '0' + remainder + 'A' - '9' + 1;
}
else
{
/* no process */
}

dest = strrev( dest );
}

void complementZeroBefore( char *src, unsigned long num )
{
unsigned long len = 0;
char temp[MAX_LENGTH] = {0};
unsigned int i = 0;

len = strlen( src );
if( len < num )
{
for( i = 0; i < ( num - len ); i++ )
{
temp[i] = '0';
}
strcat( temp, src );
memset( src, 0, len );
strcpy( src, temp );
}
else
{
/* no process */
}
}

void outputSubsetByBinary( char *src, char *binary )
{
char output[MAX_LENGTH] = {0};
int i = 0;
int index = 0;
int len = strlen( src );

for( i = 0; i < len; i++ )
{
if( binary[i] == '1' )
{
output[index] = src[i];
index++;
}
}

printf( "%s\n", output );
}
yangyang6879
2009-06-18 · 超过11用户采纳过TA的回答
知道答主
回答量:48
采纳率:0%
帮助的人:21.3万
展开全部
是随机出现吗???组合问题??
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式