这个像排列组合多点:
c:
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<math.h>
char *week[7] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }
char *names[7] = { "赵", "钱", "孙", "李", "周", "吴", "陈" }
int plan[7] = { -1 }
int planNum = 0
void doPlan(int n, int*len, int accept[7][3]) {
if (n > 6) {
planNum++
for (int i = 0 i < n ++i) {
printf(" %s\t", names[plan[i]])
}
printf("\n")
} else {
for (int i = 0 i < len[n] ++i) {
if (plan[accept[n][i]] >= 0) {
continue
}
plan[accept[n][i]] = n
doPlan(n + 1, len, accept)
plan[accept[n][i]] = -1
}
}
}
int main() {
int len[7] = { 2, 2, 2, 1, 3, 2, 3 }//这里的数字对应 accept[i] 的长度
int accept[7][3] = { { 2, 4 }, { 1, 6 }, { 0, 3 }, { 5 }, { 1, 4, 6 }, { 2,
5 }, { 0, 3, 6 } }
for (int i = 0 i < 7 i++) {
printf("%s\t", week[i])
plan[i] = -1
}
printf("\n")
doPlan(0, len, accept)
printf("总共 %d 个方案\n",planNum)
return 0
}
#include "stdafx.h"#include <iostream>
using namespace std
static char * day[] =
{
"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"
}
int _tmain(int argc, _TCHAR* argv[])
{
int a, b, c, d, e, f, g, s
d = 4
f = 1
s = 0
for( a = 0 a < 7 a++ )
{
if( (a !=1 && a != 3) || a == d || a == f )
continue
for( b = 0 b < 7 b++ )
{
if( (b != 0 && b != 5) || b == a || b == d || b == f )
continue
for( c = 0 c < 7 c++ )
{
if( (c != 2 && c != 6) || c == b || c == a || c == d || c == f )
continue
for( e = 0 e < 7 e++ )
{
if( (e != 0 && e != 3 && e != 5) || e == d || e == c || e == b || e == a || e == f)
continue
for( g = 0 g < 7 g++ )
{
if( (g != 2 && g != 5 && g != 6) || g == f || g == e || g == d || g == c || g == b || g == a )
continue
++s
cout << "Solution: " << s << endl
cout << "赵\t钱\t孙\t李\t周\t吴\t陈" << endl
cout << "==============================================================" << endl
cout << day[a] << "\t" << day[b] << "\t" << day[c] << "\t" << day[d] << "\t"
<< day[e] << "\t" << day[f] << "\t" << day[g] << endl << endl
}
}
}
}
}
}