回 帖 发 新 帖 刷新版面

主题:C++的一道简单程序题

有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

回复列表 (共15个回复)

11 楼

// 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 楼

// 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 楼

#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 楼

#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 楼

#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;
我试验了,是好使的

我来回复

您尚未登录,请登录后再回复。点此登录或注册