济南网站建设抖音平台,建设网站的功能及目的是什么,广州seo推广优化,wordpress 自学题目描述 把 1~9 这9个数字分成两组#xff0c;中间插入乘号#xff0c; 有的时候#xff0c;它们的乘积也只包含1~9这9个数字#xff0c;而且每个数字只出现1次。 比如#xff1a; 984672 * 351 345619872 98751 * 3462 341875962 9 * 87146325 784316925 … 符合这种…题目描述 把 1~9 这9个数字分成两组中间插入乘号 有的时候它们的乘积也只包含1~9这9个数字而且每个数字只出现1次。 比如 984672 * 351 345619872 98751 * 3462 341875962 9 * 87146325 784316925 … 符合这种规律的算式还有很多请你计算在所有这些算式中乘积最大是多少 输出 输出一个整数表示答案
代码如下
#include iostream
#include cstring
using namespace std;
typedef long long LL;
LL maxv -1;
const int N 12;
bool vis[N];
bool st[N];bool check(int suma, int sumc) {LL b (LL)suma * sumc;memset(vis, 0, sizeof(vis));//标记数组记得重置if (!b) {return false;}while (b) {if (vis[b % 10] || b % 10 0)return false;vis[b % 10] true;b b / 10;}for (int i 1; i 9; i) {if (!vis[i])return false;}return true;
}void dfs_c(int u, int suma, int sumc) {//if (u 9) {if (u 9) { //边界不要弄错if (check(suma, sumc)) {maxv max((LL)suma * sumc, maxv);return ;}return ;}for (int i 1; i 9; i) {if (!st[i]) {st[i] true;dfs_c(u 1, suma, sumc * 10 i);st[i] false;}}
}void dfs_a(int u, int suma) {if (u) {dfs_c(u, suma, 0);}for (int i 1; i 9; i) {if (!st[i]) {st[i] true;dfs_a(u 1, suma * 10 i);st[i] false;}}
}int main() {dfs_a(0, 0);cout maxv endl;return 0;
}
#include iostream
using namespace std;
typedef long long LL;
const int N 12;
bool vis[N];
LL maxv -1;bool check_1(int suma,int sumc)
{int st[N];memset(st,0,sizeof(st));//必不可少
// for (int i 1;i9;i)或者写成这样反正无论怎么写都要初始化为0
// {
// st[i] 0
// }LL sumb suma*(LL)sumc;while(sumb){st[sumb%10];sumb sumb/10;}for (int i 1;i9;i){if (st[i]!1)return false;}return true;
}void dfs_c(int u,int suma,int sumc)
{if (u 9) return ;if (u9){if (check_1(suma,sumc) sumc!0){LL sumb (LL)suma*sumc;maxv max(maxv,sumb);return ;}}for (int i 1;i9;i){if (!vis[i]){vis[i] true;dfs_c(u1,suma,sumc*10i);vis[i] false;}}
}void dfs_a(int u,int suma)
{dfs_c(u,suma,0);for (int i 1;i9;i){if (!vis[i]){vis[i] true;dfs_a(u1,suma*10i);vis[i] false;}}
}int main()
{dfs_a(0,0);coutmaxvendl;return 0;
}