P1008 三连击(洛谷)

题目背景

本题为提交答案题,您可以写程序或手算在本机上算出答案后,直接提交答案文本,也可提交答案生成程序。

题目描述

将1,2, \cdots ,91,2,⋯,9共99个数分成33组,分别组成33个三位数,且使这33个三位数构成1:2:31:2:3的比例,试求出所有满足条件的33个三位数。

输入输出格式

输入格式:

木有输入

输出格式:

若干行,每行33个数字。按照每行第11个数字升序排列。

输入输出样例

输入样例#1: 复制

输出样例#1: 复制

192 384 576
* * *
...

* * *
(输出被和谐了)

解析:用了一个全排列函数 next_permutation(),有两种方法,核心都一样;

(1)数组:

#include<cstdio>
#include<algorithm>
using namespace std;
int a[110];
int main()
{
    int x,y,z;
    for(int i=1;i<=9;i++)
        a[i]=i;
    do
    {
        x=a[1]*100+a[2]*10+a[3];
        y=a[4]*100+a[5]*10+a[6];
        z=a[7]*100+a[8]*10+a[9];
        if(x*2==y&&x*3==z)
        	printf("%d %d %d\n",x,y,z);
    }while(next_permutation(a+1,a+10));//10是strlen(a)的长度,包括'\0' 
    return 0;
}

(2)vector() 

#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){

    vector<int> v;
    for( int i = 1 ; i <= 9 ; i ++ )
       v.push_back(i);

    do{
        int a = v[0]*100+v[1]*10+v[2];
        int b = v[3]*100+v[4]*10+v[5];
        int c = v[6]*100+v[7]*10+v[8];

        if( a*2 == b && a*3 == c )
            printf("%d %d %d\n",a , b , c );

    }while( next_permutation( v.begin() , v.end() ));

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41555192/article/details/82388373