解法:全排列暴力枚举
思路:递归模拟全排列1-9,9位数的所有排列形式,再对每一个9位数进行组合划分为三个数,a,b,c只要满足目标数N=a+b/c成立即可。
组合数划分:对全排列的9位数只需用两个隔板隔离成三个连续的数即可。如a:1-2位,b:3-6位,c:7-9位等。从左到右分,第一个隔板从1位-7位,第二个隔板:从第一个隔板+1位—第8位.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; //枚举全排列,枚举出所有可能的9位数然后9个数划分为三个数abc,n=a+b/c int st[10]; bool used[10]; int cal(int *st,int l,int r) { int ans=0;int x=1; for(int i=r;i>=l;i--) { ans=ans+x*st[i]; x=x*10; } return ans; } int fans=0; void dfs(int x,int n) { if(x>9){ //对9个数进行组合划分成3个数,只考虑位置不考虑顺序 for(int i=1;i<=7;i++) { for(int j=i+1;j<=8;j++) { int a=cal(st,1,i);//1-2;1-3;1-4。。。1-7 int b=cal(st,i+1,j); int c=cal(st,j+1,9); if(n*c==a*c+b) { fans++; // cout<<n<<"="<<a<<"+"<<b<<"/"<<c<<endl; } } } return; } for(int i=1;i<=9;i++) { if(used[i]==false) { st[x]=i; used[i]=true; dfs(x+1,n); used[i]=false; } } } int main() { int n;cin>>n; dfs(1,n); cout<<fans; return 0; }
蓝桥杯真题带分数
最新推荐文章于 2024-07-25 13:44:24 发布