穷举的头都晕了,还是吧数字转换成字符吧...
#include
using namespace std;
const int Mon = 1;
const int Tues = 2;
const int Wed = 3;
const int Thus = 4;
const int Fri = 5;
const int Sat = 6;
const int Sun = 7;
void change(int a)
{
switch(a)
{
case 1: cout << "一" << endl; break;
case 2: cout << "二" << endl; break;
case 3: cout << "三" << endl; break;
case 4: cout << "四" << endl; break;
case 5: cout << "五" << endl; break;
case 6: cout << "六" << endl; break;
case 7: cout << "日" << endl; break;
}
}
int main()
{
int o1[2] = {Tues, Thus},o2[2] = {Mon, Sat},o3[2] = {Wed, Sun},o4[1] = {Fri},o5[3] = {Mon, Thus, Sat},o6[2] = {Tues, Fri},o7[3] = {Wed, Sat, Sun};
int i,j,k,x,a,b,c, n[7], flag, lag;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
if(o1[i] == o2[j])
continue;
for(k = 0; k < 2; k++)
{
if(o2[j] == o3[k])
continue;
for(x = 0; x < 1; x++)
{
if(o3[k] == o4[x])
continue;
for(a = 0; a < 3; a++)
{
if(o4[x] == o5[a])
continue;
for(b = 0; b < 2; b++)
{
if(o5[a] == o6[b])
continue;
for(c = 0; c < 3; c++)
{
if(o6[b] == o7[c])
continue;
if(o1[i]+o2[j]+o3[k]+o4[x]+o5[a]+o6[b]+o7[c] == 28)
{
n[0] = o1[i];
n[1] = o2[j];
n[2] = o3[k];
n[3] = o4[x];
n[4] = o5[a];
n[5] = o6[b];
n[6] = o7[c];
for(flag = 0; flag < 7; flag++)
{
for(lag = 0; lag < flag; lag++)
{
if(n[flag] == n[lag])
break;
}
if(lag != flag)
break;
}
if(lag != flag && flag != 7)
continue;
cout << "和尚1 星期"; change(o1[i]);
cout << "和尚2 星期"; change(o2[j]);
cout << "和尚3 星期"; change(o3[k]);
cout << "和尚4 星期"; change(o4[x]);
cout << "和尚5 星期"; change(o5[a]);
cout << "和尚6 星期"; change(o6[b]);
cout << "和尚7 星期"; change(o7[c]);
cout << endl;
}
}
}
}
}
}
}
}
return 0;
}
颠三倒四 撒萨大声
给你思路自己做,第一个.
7个2进制代表和尚在一周中在那一天挑水.然后后面的条件表示出来.最后用if语句就可以了.
#include
void main()
{
int a1,a2,a3,a4,a5,a6,a7;
for(a1=1;a1<=7;a1++)
{
if(a1==2) continue;
if(a1==4) continue;
printf("a1=%d, ",a1);
for(a2=2;a2<=7;a2++)
{
if(a2==a1) continue;
if(a2==6) continue;
printf("a2=%d, ",a2);
for(a3=1;a3<=7;a3++)
{
if(a3==a1) continue;
if(a3==a2) continue;
if(a3==3) continue;
if(a3==7) continue;
printf("a3=%d, ",a3);
for(a4=1;a4<=7;a4++)
{
if(a4==a1) continue;
if(a4==a2) continue;
if(a4==a3) continue;
if(a4==5) continue;
printf("a4=%d, ",a4);
for(a5=1;a5<=7;a5++)
{
if(a5==a1) continue;
if(a5==a2) continue;
if(a5==a3) continue;
if(a5==a4) continue;
if(a5==1) continue;
if(a5==4) continue;
if(a5==6) continue;
printf("a5=%d, ",a5);
for(a6=1;a6<=7;a6++)
{
if(a6==a1) continue;
if(a6==a2) continue;
if(a6==a3) continue;
if(a6==a4) continue;
if(a6==a5) continue;
if(a6==2) continue;
if(a6==5) continue;
printf("a6=%d, ",a6);
for(a7=1;a7<=7;a7++)
{
if(a7==a1) continue;
if(a7==a2) continue;
if(a7==a3) continue;
if(a7==a4) continue;
if(a7==a5) continue;
if(a7==a6) continue;
if(a7==3) continue;
if(a7==6) continue;
if(a7==7) continue;
printf("a7=%d, ",a7);
}
}
}
}
}
}
}
}
输到文件你自己弄了!~1
改下 这个是输出到out.txt文件的
#include
#include
#include
#include
using namespace std;
int main()
{
set
int day[7],i;
int he[7][3]={{2,4,-1},{1,6,-1},{3,7,-1},{5,-1,-1},{1,4,6},{2,5,-1},{3,6,7}};
string chr[7]={"一","二","三","四","五","六","日"};
ofstream f1("out.txt");
for (day[0]=0;day[0]<3;day[0]++)
for (day[1]=0;day[1]<3;day[1]++)
for (day[2]=0;day[2]<3;day[2]++)
for (day[3]=0;day[3]<3;day[3]++)
for (day[4]=0;day[4]<3;day[4]++)
for (day[5]=0;day[5]<3;day[5]++)
for (day[6]=0;day[6]<3;day[6]++)
{
data.clear();
for (i=0;i<7;i++)
{
if (he[i][day[i]]>0)
data.insert(he[i][day[i]]);
}
if (7==data.size())
{
f1<<"============方案==============="<
f1<<"和尚"< }
}
f1.close();
return 0;
}
用for循环+if判断编的,虽然有点麻烦,但是通俗易懂,其实你再可以通过逻辑简化的
#include
void main()
{
int a[7]={1,2,3,4,5,6,7},i[7],b,c,d,e,f,g,h;
for(b=0;b<=6;b++)
{
for(c=0;c<=6;c++)
{
for(d=0;d<=6;d++)
{
for(e=0;e<=6;e++)
{
for(f=0;f<=6;f++)
{
for(g=0;g<=6;g++)
{
for(h=0;h<=6;h++)
{
i[0]=a[b];
i[1]=a[c];
i[2]=a[d];
i[3]=a[e];
i[4]=a[f];
i[5]=a[g];
i[6]=a[h]; // 对1~7做全排列
if(b==c||b==d||b==e||b==f||b==g||b==h||c==d||c==e||c==f||c==g||c==h||d==e||d==f||d==g||d==h||e==f||e==g||e==h||f==g||f==h||g==h)continue; //保证没有重复的数字
if(i[0]!=2&&i[0]!=5)continue;
if(i[1]!=1&&i[1]!=6)continue;
if(i[2]!=3&&i[2]!=7)continue;
if(i[3]!=1&&i[3]!=5)continue;
if(i[4]!=4&&i[4]!=6)continue;
if(i[5]!=2&&i[5]!=5&&i[5]!=7)continue;
if(i[6]!=3&&i[6]!=7)continue; //判断是否符合要求
printf("星期一 星期二 星期三 星期四 星期五 星期六 星期日\n");
printf("和尚%d 和尚%d 和尚%d 和尚%d 和尚%d 和尚%d 和尚%d\n",i[0],i[1],i[2],i[3],i[4],i[5],i[6]); //若符合要求则输出
}
}
}
}
}
}
}