zl程序教程

您现在的位置是:首页 >  其它

当前栏目

蓝桥杯打卡第四天

蓝桥 打卡 第四天
2023-09-11 14:20:29 时间

第一题:

题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
观察如下的算式:
9213×85674=789314562
左边的乘数和被乘数正好用到了 1 ~ 9的所有数字,每个 1 次。 而乘积恰好也是用到了 1 ~ 9 的所有数字,并且每个 1 次。
请你借助计算机的强大计算能力,找出满足如上要求的 9 数算式一共有多少个?
注意:
总数目包含题目给出的那个示例。
乘数和被乘数交换后作为同一方案来看待。
运行限制
最大运行时间:1s
最大运行内存: 128M

全排列

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 11;
int ans, s[N];

bool check(int x){
  memset(s, 0, sizeof s);

  while(x){
    s[x % 10] ++;
    x /= 10;
  }

  for(int i = 1; i <= 9; i++)
    if(s[i] != 1) return false;
  return true;
}

int main(){
  string s = "123456789";
  do{
    for(int i = 1; i <= 8; i++){
      int x = stoi(s.substr(0, i));
      int y = stoi(s.substr(i));
      if(check(x * y))  ans++;
    }
  }while(next_permutation(s.begin(), s.end()));

  cout<<ans / 2<<endl;
  return 0;
}