算法提高 连接乘积

    xiaoxiao2021-03-25  114

    算法提高 连接乘积   时间限制:1.0s   内存限制:256.0MB      问题描述   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

    #include <stdio.h>   #include <stdlib.h>   #include <string.h>  #include "iostream" #include "sstream" #include "string" #include "queue" #include "algorithm" typedef long long LL; using namespace std; int Array[9] = { 1,2,3,4,5,6,7,8,9 }; long long val( int b) { long long x=0; for (int i = 0; i < b; i++) x = x * 10 + Array[i]; return x; } bool check() { stringstream t; t << val(9); string ss=t.str(); for (int i = 1; i < 9; i++) { string s2; int x = 1; long long x1 = val( i); while(s2.size()<9) { stringstream m; m<<x1*x++; s2 += m.str(); } //cout << ss << "  " << s2 << endl; if (s2 == ss)return true; } return false; } int main() { do { if (check()) { for (int i = 0; i < 9; i++) { printf("%d", Array[i]); } printf("\n"); } } while (next_permutation(Array, Array + 9)); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-5078.html

    最新回复(0)