蓝桥 - 连接乘积 暴力

192这个数很厉害,用它分别乘以1、2、3,会得到:
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
把这三个乘积连起来,得到192384576,正好是一个1~9的全排列
我们把上面的运算定义为连接乘积:
m x (1 … n) = k(其中m > 0 且 n > 1,对于上例,m = 192、n = 3、k = 192384576)
即k是把m分别乘以1到n的乘积连接起来得到的,则称k为m和n的连接乘积。

按字典序输出所有不同的连接乘积k,满足k是1~9的全排列

输出格式
每个k占一行

显然,结果中应包含一行:
192384576

思路:按题目描述的暴力找就行 排好序写个cpp输出即可

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
bool judge(int num){
    int con[10];
    cl(con,0);
    int t=1;
    while(1){
        int tp=num*t;
        while(tp){
            if(tp%10==0) return false;
            con[tp%10]++;
            tp/=10;
        }
        if(con[1]==1&&con[2]==1&&con[3]==1&&con[4]==1&&con[5]==1&&con[6]==1&&con[7]==1&&con[8]==1&&con[9]==1){
            return true;
        }else{
            rep(i,0,9){
                if(con[i]>1) return false;
            }
        }
        t++;
    }
}
inline int getnum(int num){
    if(num==0) return 1;
    int ans=0;
    while(num){
        ans++;
        num/=10;
    }
    return ans;
}
int main() {
	rep(i,1,100000){
        if(judge(i)){
            int t=1;
            int num=0;
            while(1){
                printf("%d",i*t);
                num+=getnum(i*t);
                if(num==9){
                    printf("\n");
                    break;
                }
                t++;
            }
        }
    }
	return 0;
}

下面是输出的代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
	printf("123456789\n");
	printf("192384576\n");
	printf("219438657\n");
	printf("273546819\n");
	printf("327654981\n");
	printf("672913458\n");
	printf("679213584\n");
	printf("692713854\n");
	printf("726914538\n");
	printf("729314586\n");
	printf("732914658\n");
	printf("769215384\n");
	printf("792315846\n");
	printf("793215864\n");
	printf("918273645\n");
	printf("926718534\n");
	printf("927318546\n");
	printf("932718654\n");
	return 0;
}
发布了120 篇原创文章 · 获赞 12 · 访问量 5266

猜你喜欢

转载自blog.csdn.net/weixin_43735161/article/details/104986464