
帮忙解决一道简单C++题目
书架上有21本书,编号从1到21,从其中选4本,其中每两本的编号都不相邻的选法一共有多少种?编的程序如下:这样符合题意吗?#include<iostream>usingn...
书架上有21本书,编号从1到21,从其中选4本,其中每两本的编号都不相邻的选法一共有多少种?
编的程序如下:
这样符合题意吗?
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e=0;
for(a=1;a<22;++a)
{
for(b=1;b<22;++b)
{
{
if(!((a-b)>1||(a-b)<1)){continue;}
for(c=1;c<22;++c)
{
if(!(((a-c)>1||(a-c)<-1)||((b-c)>1||(b-c)<-1))){continue;}
for(d=1;d<22;++d)
{
if(!(((a-d)>1||(a-d)<-1)||((b-d)>1||(b-d)<-1)||((c-d)>1||(c-d)<-1)))
{continue;}
e++;
}
}
}
}
}
cout<<e/2;
system("pause");
return 0;
}
不用程序在实际中又怎样计算。
在实际中怎样计算?
结果又是多少?
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
要详细的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 展开
编的程序如下:
这样符合题意吗?
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e=0;
for(a=1;a<22;++a)
{
for(b=1;b<22;++b)
{
{
if(!((a-b)>1||(a-b)<1)){continue;}
for(c=1;c<22;++c)
{
if(!(((a-c)>1||(a-c)<-1)||((b-c)>1||(b-c)<-1))){continue;}
for(d=1;d<22;++d)
{
if(!(((a-d)>1||(a-d)<-1)||((b-d)>1||(b-d)<-1)||((c-d)>1||(c-d)<-1)))
{continue;}
e++;
}
}
}
}
}
cout<<e/2;
system("pause");
return 0;
}
不用程序在实际中又怎样计算。
在实际中怎样计算?
结果又是多少?
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
请说明详细一点,多谢!
要详细的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 展开
3个回答
展开全部
选1 3 5 7
与1 5 7 3
是不是同一种?
如果是的话,你的程序就重复了。
这是常规的排列组合题。
#include <stdio.h>
void main()
{
int i,j,k,l;long int js;
js=0;
for (i=1;i<=21;i++)
for (j=i+2;j<=21;j++)
for (k=j+2;k<=21;k++)
for (l=k+2;l<=21;l++)
{printf("%i %i %i %i\n",i,j,k,l);js++;
}
printf("js=%i",js);
}
实际计算:
18=21-3
这里3是卡在中间的书。
例如:
去掉这三本书,编号为1 2 3 4,则加入这三本书编号为1 3 5 7
去掉这三本书,编号为1 2 17 18,则加入这三本书编号为1 3 19 21
18本书任取4本书为:
18*17*16*15/4/3/2/1=3060
与1 5 7 3
是不是同一种?
如果是的话,你的程序就重复了。
这是常规的排列组合题。
#include <stdio.h>
void main()
{
int i,j,k,l;long int js;
js=0;
for (i=1;i<=21;i++)
for (j=i+2;j<=21;j++)
for (k=j+2;k<=21;k++)
for (l=k+2;l<=21;l++)
{printf("%i %i %i %i\n",i,j,k,l);js++;
}
printf("js=%i",js);
}
实际计算:
18=21-3
这里3是卡在中间的书。
例如:
去掉这三本书,编号为1 2 3 4,则加入这三本书编号为1 3 5 7
去掉这三本书,编号为1 2 17 18,则加入这三本书编号为1 3 19 21
18本书任取4本书为:
18*17*16*15/4/3/2/1=3060
展开全部
#include <iostream>
using namespace std;
int main(){
int a,b,c,d,e=0;
for(a=1;a<22;a++){
for(b=a+1;b<22;b++){
if(abs(b-a) > 1){
for(c=b+1;c<22;c++){
if(abs(c-b) > 1){
for(d=c+1;d<22;d++){
if(abs(d-c) > 1){
//cout << a << " " << b << " " << c << " " << d << endl;
e++;
}
}
}
}
}
}
}
cout << e;
system("pause");
return 0;
}
这个是枚举法
using namespace std;
int main(){
int a,b,c,d,e=0;
for(a=1;a<22;a++){
for(b=a+1;b<22;b++){
if(abs(b-a) > 1){
for(c=b+1;c<22;c++){
if(abs(c-b) > 1){
for(d=c+1;d<22;d++){
if(abs(d-c) > 1){
//cout << a << " " << b << " " << c << " " << d << endl;
e++;
}
}
}
}
}
}
}
cout << e;
system("pause");
return 0;
}
这个是枚举法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int count_Combination_NoNeighboring(int n, int r)
源程序请参见
源程序请参见
参考资料: http://blog.csdn.net/n43e120/archive/2010/02/03/5284557.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询