65,201
社区成员




#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,n=0;
char c[5][7]={"red","yellow","blue","white","black"};
for (i=0;i<5;i++)
for (j=i+1;j<5;j++)
for (k=j+1;k<5;k++)
{
if (i != j && j !=k && k!=i)
printf("%s %s %s\n",c[i],c[j],c[k]);
n++;
}
printf("n=%d",n);
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define N 7
main(void)
{
enum color{red,yellow,blue,white,black};//五种颜色
enum color pri;
int n,loop,i,j,k;
char c;
n=0;
for(i=red;i<=black;i++) //算法开始
{
for(j=red;j<=black;j++)
{
if(i!=j)
{
for(k=red;k<=black;k++)
{
if((k!=i)&&(k!=j))
{
n=n+1;
cout<<setw(2)<<n;
for(loop=1;loop<=3;loop++)
{
switch(loop) //取值
{
case 1:pri=(enum color)i;
break;
case 2:pri=(enum color)j;
break;
case 3:pri=(enum color)k;
break;
default:break;
}
switch(pri) //输出球的颜色
{
case red:cout<<setw(N)<<"red";break;
case yellow:cout<<setw(N)<<"yellow";break;
case blue:cout<<setw(N)<<"blue";break;
case white:cout<<setw(N)<<"white";break;
case black:cout<<setw(N)<<"black";break;
default:break;
}
}
cout<<endl;
}
}
}
}
}
cout<<"total :"<<n<<endl; //输出总数
getch();
}
3
1 2 3 4 5
红:0 黄:0 蓝:0 白:0 黑:3
红:0 黄:0 蓝:0 白:1 黑:2
红:0 黄:0 蓝:0 白:2 黑:1
红:0 黄:0 蓝:0 白:3 黑:0
红:0 黄:0 蓝:1 白:0 黑:2
红:0 黄:0 蓝:1 白:1 黑:1
红:0 黄:0 蓝:1 白:2 黑:0
红:0 黄:0 蓝:2 白:0 黑:1
红:0 黄:0 蓝:2 白:1 黑:0
红:0 黄:0 蓝:3 白:0 黑:0
红:0 黄:1 蓝:0 白:0 黑:2
红:0 黄:1 蓝:0 白:1 黑:1
红:0 黄:1 蓝:0 白:2 黑:0
红:0 黄:1 蓝:1 白:0 黑:1
红:0 黄:1 蓝:1 白:1 黑:0
红:0 黄:1 蓝:2 白:0 黑:0
红:0 黄:2 蓝:0 白:0 黑:1
红:0 黄:2 蓝:0 白:1 黑:0
红:0 黄:2 蓝:1 白:0 黑:0
红:1 黄:0 蓝:0 白:0 黑:2
红:1 黄:0 蓝:0 白:1 黑:1
红:1 黄:0 蓝:0 白:2 黑:0
红:1 黄:0 蓝:1 白:0 黑:1
红:1 黄:0 蓝:1 白:1 黑:0
红:1 黄:0 蓝:2 白:0 黑:0
红:1 黄:1 蓝:0 白:0 黑:1
红:1 黄:1 蓝:0 白:1 黑:0
红:1 黄:1 蓝:1 白:0 黑:0
红:1 黄:2 蓝:0 白:0 黑:0
请按任意键继续. . .
#include <iostream>
using namespace std;
const int NCOLOR=5; // 5 colors
const char* ballName[]={"红","黄","蓝","白","黑"};
int count[NCOLOR]; // count of each color balls ,list: 红,黄,蓝,白,黑
int result[NCOLOR]; // a choice result
int sumAfter[NCOLOR+1]; // sum of count of each color from i to NCOLOR
int n; // to take out n balls
void printResult(int cur,int total)
{
if(cur==NCOLOR)
{
if(total==n)
{
for(int i=0;i<NCOLOR;++i)
{
cout<<ballName[i]<<":"<<result[i]<<"\t";
}
cout<<endl;
}
return;
}
for(int cnt=0;cnt<=count[cur] && cnt+total<=n;++cnt) //剪枝1
{
if(cnt+total+sumAfter[cur+1]>=n) //剪枝2
{
result[cur]=cnt;
printResult(cur+1,total+cnt);
}
}
}
int main()
{
cin>>n;
for(int i=0;i<NCOLOR;++i)
{
cin>>count[i];
}
sumAfter[NCOLOR]=0;
for(int i=NCOLOR-1;i>=0;--i)
{
sumAfter[i]=sumAfter[i+1]+count[i];
}
printResult(0,0);
return 0;
}
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define N 7
main(void)
{
enum color{red = 1,yellow,blue,white,black};//五种颜色
enum color pri;
int n,loop,i,j,k;
char c;
n=0;
for(i=red;i<=black;i++) //算法开始
{
for(j=i;j<=black;j++)
{
if(i!=j)
{
for(k=j;k<=black;k++)
{
if((k!=i)&&(k!=j))
{
n=n+1;
cout<<setw(2)<<n;
for(loop=1;loop<=3;loop++)
{
switch(loop) //取值
{
case 1:pri=(enum color)i;
break;
case 2:pri=(enum color)j;
break;
case 3:pri=(enum color)k;
break;
default:break;
}
switch(pri) //输出球的颜色
{
case red:cout<<setw(N)<<"red";break;
case yellow:cout<<setw(N)<<"yellow";break;
case blue:cout<<setw(N)<<"blue";break;
case white:cout<<setw(N)<<"white";break;
case black:cout<<setw(N)<<"black";break;
default:break;
}
}
cout<<endl;
}
}
}
}
}
cout<<"total :"<<n<<endl; //输出总数
getch();
}
//这个就是一样的 没考虑顺序的