不知道错在哪里了,请解答

lhjtc8257 2008-08-02 03:10:58
题目:0,1,2,3,4,5排列4位数字,要求无重复,为什么会是630,应该为150的?
求解答

#include <stdio.h>
main(void)
{
int a,b,c,d,count=0;
for (a=0;a<=5;a++)
for (b=0;b<=5;b++)
for (c=0;c<=5;c++)
for (d=0;d<=5;d++)
{
// printf("%d\n",count);
if (a!=b && b!=c && c!=d && d!=a)
{
//printf("number=%d\n",(a*1000+b*100+c*10+d));
count=count+1;
}
}
printf("%d\n",count);
}
...全文
111 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhjtc8257 2008-08-02
  • 打赏
  • 举报
回复
是,4位排列,但不能有重复的
请问错在哪里!
guzhilei1986 2008-08-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangdeqie 的回复:]
C/C++ code//这个不错,不过是C++的#include<iostream>#include<algorithm>usingnamespacestd;//递归产生R[k:n]的所有的排列,元素是互异的template<classType>voidpermutation(Type*R,intk,intn)
{if(k==n)
{for(inti=0;i<n;++i)
cout<<R[i]<<"\t";
cout<<endl;
}elsefor(inti=k;i<n;++i)
{
swap(R[k],R[i]);
permutation(R,k+1,n);
swap(R[k],R[i]);…
[/Quote]
沸顶
全排列
PLigt 2008-08-02
  • 打赏
  • 举报
回复
首先 a!=b && b!=c && c!=d &&d!=a 无法推出 a!=c 和 b!=d
另外不知道LZ是不是排列4位数如果是的话:
#include <stdio.h>
//#include <fstream.h>
main(void)
{
char str[20]="";
//ofstream ofs("D:/Debug.txt");
int a,b,c,d,count=0;
for (a=0;a <= 5;a++)
for (b=0;b <= 5;b++)
for (c=0;c <= 5;c++)
for (d=0;d <= 5;d++)
{
if (a!=b && b!=c && c!=d && d!=a && a!=c && b!=d && a!=0)
{
//sprintf(str,"number=%d\n",(a*1000+b*100+c*10+d));
//ofs<<str;
count=count+1;
}
}
printf("%d\n",count);
}
我估计LZ是想把表打下来,但是显示不全,把我的注释去掉就可以在D盘下找到输出列表,不过好像是C++的。
wangdeqie 2008-08-02
  • 打赏
  • 举报
回复

//这个不错,不过是C++的
#include < iostream >
#include < algorithm >

using namespace std;

// 递归产生R[k:n]的所有的排列,元素是互异的
template < class Type >
void permutation(Type * R, int k, int n)
{
if (k == n)
{
for ( int i = 0 ;i < n; ++ i)
cout << R[i] << " \t " ;
cout << endl;
}
else
for ( int i = k;i < n; ++ i)
{
swap(R[k],R[i]);
permutation(R,k + 1 ,n);
swap(R[k],R[i]);
}
}
void main()
{
int a[3]={1,2,3};
permutation(a,0,3);
}
brookmill 2008-08-02
  • 打赏
  • 举报
回复
计算6取4的排列,应该是6*5*4*3 = 360
去掉0开头的,应该是360-5*4*3 = 300
唉,数学全忘了,不知道对不对
brookmill 2008-08-02
  • 打赏
  • 举报
回复
如果改成这样:
if (a!=b && b!=c && c!=d && d!=a && a!=c && b!=d)
结果是360

如果改成这样:
if (a!=b && b!=c && c!=d && d!=a && a!=c && b!=d && a!=0)
结果是300

LZ为什么认为是150呢?

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧