C语言如何实现组合后多条匹配条件过滤输出问题用C语言同时满足下列2个条件就输出源代码(1)假定从1-15这15个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)(2)将第一
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/25 02:24:50
C语言如何实现组合后多条匹配条件过滤输出问题用C语言同时满足下列2个条件就输出源代码(1)假定从1-15这15个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)(2)将第一
C语言如何实现组合后多条匹配条件过滤输出问题
用C语言同时满足下列2个条件就输出源代码
(1)假定从1-15这15个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)
(2)将第一问得到数据过滤,当每一行6个数字含有下列9条4码中的2至5条(最少含有2条最多5条)就输出,结果共有62组就正确
01,02,03,06
01,02,03,08
01,02,03,09
02,05,06,08
02,06,07,08
05,06,07,09
06,08,09,10
08,09,10,11
06,07,08,13
C语言如何实现组合后多条匹配条件过滤输出问题用C语言同时满足下列2个条件就输出源代码(1)假定从1-15这15个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)(2)将第一
#include <stdio.h>
#include <string.h>
FILE *fp = NULL;
int requirement[9][16];
int num[6];
void init()
{
memset(requirement,0,sizeof(requirement));
requirement[0][1] = requirement[0][2] = requirement[0][3] = requirement[0][6] = 1;
requirement[1][1] = requirement[1][2] = requirement[1][3] = requirement[1][8] = 1;
requirement[2][1] = requirement[2][2] = requirement[2][3] = requirement[2][9] = 1;
requirement[3][2] = requirement[3][5] = requirement[3][6] = requirement[3][8] = 1;
requirement[4][2] = requirement[4][6] = requirement[4][7] = requirement[4][8] = 1;
requirement[5][5] = requirement[5][6] = requirement[5][7] = requirement[5][9] = 1;
requirement[6][6] = requirement[6][8] = requirement[6][9] = requirement[6][10] = 1;
requirement[7][8] = requirement[7][9] = requirement[7][10] = requirement[7][11] = 1;
requirement[8][6] = requirement[8][7] = requirement[8][8] = requirement[8][13] = 1;
}
int check()
{
int total = 0,i,tmp,j;
for(i=0;i<9;i++)
{
tmp = 0;
for(j=0;j<6;j++)
tmp += requirement[i][ num[j] ];
if(tmp == 4)
total++;
}
if(total >= 2 && total <= 5)
return 1;
return 0;
}
void output()
{
fprintf( fp , "%02d,%02d,%02d,%02d,%02d,%02d\n" , num[0] , num[1] , num[2] , num[3] , num[4] , num[5] );
}
void dfs(int dep,int lastnum)
{
if(dep == 6)
{
if(check())
{
output();
}
return ;
}
int i;
for(i=lastnum + 1 ; i<=15;i++)
{
num[dep] = i;
dfs(dep+1,i);
}
}
int main()
{
fp = fopen("C://62.txt","w");
if(fp == NULL)
{
printf("open file error!\n");
return 0;
}
init();
dfs(0,0);
return 0;
}
输出:
01,02,03,04,06,0801,02,03,04,06,09
01,02,03,04,08,09
01,02,03,05,06,08
01,02,03,05,06,09
01,02,03,05,08,09
01,02,03,06,07,08
01,02,03,06,07,09
01,02,03,06,08,09
01,02,03,06,08,10
01,02,03,06,08,11
01,02,03,06,08,12
01,02,03,06,08,13
01,02,03,06,08,14
01,02,03,06,08,15
01,02,03,06,09,10
01,02,03,06,09,11
01,02,03,06,09,12
01,02,03,06,09,13
01,02,03,06,09,14
01,02,03,06,09,15
01,02,03,07,08,09
01,02,03,08,09,10
01,02,03,08,09,11
01,02,03,08,09,12
01,02,03,08,09,13
01,02,03,08,09,14
01,02,03,08,09,15
01,02,05,06,07,08
01,02,06,07,08,13
01,06,08,09,10,11
02,03,05,06,07,08
02,03,06,07,08,13
02,04,05,06,07,08
02,04,06,07,08,13
02,05,06,07,08,09
02,05,06,07,08,10
02,05,06,07,08,11
02,05,06,07,08,12
02,05,06,07,08,13
02,05,06,07,08,14
02,05,06,07,08,15
02,05,06,08,09,10
02,06,07,08,09,10
02,06,07,08,09,13
02,06,07,08,10,13
02,06,07,08,11,13
02,06,07,08,12,13
02,06,07,08,13,14
02,06,07,08,13,15
02,06,08,09,10,11
03,06,08,09,10,11
04,06,08,09,10,11
05,06,07,08,09,10
05,06,07,08,09,13
05,06,08,09,10,11
06,07,08,09,10,11
06,07,08,09,10,13
06,08,09,10,11,12
06,08,09,10,11,13
06,08,09,10,11,14
06,08,09,10,11,15