主题:C++的一道简单程序题
xuexinlong
[专家分:0] 发布于 2008-06-24 15:44:00
有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
回复列表 (共15个回复)
11 楼
baimeigang [专家分:30] 发布于 2009-03-12 11:04:00
// IT'S EASY!
// cppro2.cpp: 主项目文件。
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int i,j,k,count;
count=0;
Console::WriteLine("这些数字是以下排列:");
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
if ((i!=j)&&(i!=k)&&(j!=k))
{
Console::Write(i);
Console::Write(j);
Console::Write(k);
Console::WriteLine();
count++;
}
}
}
}
Console::Write("这些数字的排列个数是:",count);
Console::Write(count);
Console::ReadLine();
return 0;
}
12 楼
baimeigang [专家分:30] 发布于 2009-03-12 11:09:00
// cppro2.cpp: 主项目文件。
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int i,j,k,l,count;
count=0;
Console::WriteLine("这些数字是以下排列:");
for(l=0;l<=4;l++)
{
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
if ((i!=j)&&(i!=k)&&(j!=k))
{
Console::Write(l);
Console::Write(i);
Console::Write(j);
Console::Write(k);
Console::WriteLine();
count++;
}
}
}
}
}
Console::Write("这些数字的排列个数是:",count);
Console::Write(count);
Console::ReadLine();
return 0;
}
13 楼
chenjieysu [专家分:0] 发布于 2009-03-13 19:14:00
#include<iostream>
using namespace std;
void main()
{
int i,j,k;//分别代表三位数的个,十,百位
int n;//被测试的三位数
for(n=100;n!=1000;n++)
{ i=n%10;
j=(n%100)/10;
k=n/100;
if((i!=j&&j!=k&&i!=k)&&(i==1||i==2||i==3||i==4)&&(j==1||j==2||j==3||j==4)&&(k==1||k==2||k==3||k==4))
cout<<n<<endl;}
}结果是:
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
Press any key to continue如果要统计个数的话加以个int型变量count初始化为0,输出一个n就count++,最后输出count就可以了。
14 楼
spacse [专家分:80] 发布于 2009-03-14 00:38:00
#include "iostream.h"
void main()
{
int i,j,k;
int count =0;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
if(i!=j && j!=k && k!=i)
{
cout << (i*100 + j*10 +k) << endl;
count ++;
}
}
}
}
cout << count <<endl;
}
15 楼
Frostykiss [专家分:0] 发布于 2009-03-14 00:49:00
#include<iostream.h>
void main()
{int a,b,c,n,k=0;
for (a=1;a<=4;a++)
for (b=1;b<=4;b++)
for (c=1;c<=4;c++)
{if (a!=b&&b!=c&&a!=c){ n=a*100+ b*10+c;
k++;
cout<<n<<endl;}
}
cout<<"总数为:"<<k<<endl;
我试验了,是好使的
我来回复