茶颜悦色计划
茶颜悦色计划
故事背景
今天上乐林的时候,我的老师sxn对着一道算式谜说:这题有多解,但是我只能想到
行动
于是,我想着用圆珠笔的超高情商去让我和xyy打好关系,于是……我便开始想办法。
题解
第一步,暴力,时间复杂度:
第二步,优化。我们可以应deepth first search来实现枚举全排列,时间复杂度
代码实现
代码是这样的:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int vis[10];
int now[10];
int cnt=0;
void dfs(int step){
if(step==10){
if(now[1]*now[2]-now[3]==(now[4]*10+now[5])/(now[6]*10+now[7])+now[8] and (now[4]*10+now[5])/(now[6]*10+now[7])+now[8]==now[9] and (now[4]*10+now[5])%(now[6]*10+now[7])==0){
printf("%lldx%lld-%lld=%lld%lld/%lld%lld+%lld=%lld\n",now[1],now[2],now[3],now[4],now[5],now[6],now[7],now[8],now[9]);
cnt++;
}
return;
}
for(int i = 1;i <= 9;i++){
if(vis[i]) continue;
vis[i]=1;
now[step]=i;
dfs(step+1);
vis[i]=0;
}
}
signed main(){
cout<<"The answers are:"<<endl;
dfs(1);
cout<<"There are "<<cnt<<" answers"<<endl;
return 0;
}
相信就算是OMER也能看懂,dogeeeeeeeeeee。
结果
最后就是,老师希望第五个,结果是:(包括老师讲的四个)
The answers are:
1x7-2=96/48+3=5
1x9-2=68/34+5=7
1x9-2=86/43+5=7
1x9-5=76/38+2=4
2x5-1=68/34+7=9
2x5-1=86/43+7=9
2x5-7=96/48+1=3
2x6-3=85/17+4=9
2x8-7=65/13+4=9
2x8-9=56/14+3=7
3x5-6=28/14+7=9
3x5-6=82/41+7=9
5x2-1=68/34+7=9
5x2-1=86/43+7=9
5x2-7=96/48+1=3
5x3-6=28/14+7=9
5x3-6=82/41+7=9
6x2-3=85/17+4=9
7x1-2=96/48+3=5
8x2-7=65/13+4=9
8x2-9=56/14+3=7
9x1-2=68/34+5=7
9x1-2=86/43+5=7
9x1-5=76/38+2=4
There are 24 answers
好家伙,足足