有一个问题:
4个人相互传球,由甲开始发球,并作为第一次传球,经过4次传球后,球仍回到甲手中。
那么,一共有多少种传球方式?(可以有人未接到球)。
对于这个问题用C++进行求解:
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int a=1;
int b=1;
int c=1;
int sumx=0;
for(a=1;a<5;a++)
for(b=1;b<5;b++)
for(c=1;c<5;c++)
{
//进行判断,第一次传球不能是甲接收,第二次传球接收者不能是第一次的人,第三次传球接收者不能是第二次的人
//第三次传球的接收者不可以是甲,否则第四次传球,不能出现甲传递给自己。
if(a!=1 && b!=a && c!=b && c!=1)
{
sumx++;
cout<< "第" <<sumx << "种方法: " ;
cout << 1 <<">" << a <<">" << b <<">" << c << " > 1" << endl;
}
}
return 0;
}