C语言编程,题:输入M个不同的字符,从中选出N个字符,输出所有可能的方案。M,N由键盘输入.xie
2个回答
展开全部
代码如下(最多支持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 );
}
#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 );
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询