·今天是2019年8月7日啊,祝各位大佬七夕快乐~~~
·这题这种解法完全就是暴力嘛。。。好像还有搜索解法(以后再更)。
·洛谷入口:P1149
·题目背景:2008年NOIP提高组真题
·题目内容:
给你n根火柴棍,你可以拼出多少个形如“A+B=CA+B=C”的等式?等式中的AA、BB、CC是用火柴棍拼出的整数(若该数非零,则最高位不能是00)。用火柴棍拼数字0-90−9的拼法如图所示:
·题目注意事项:
1.加号与等号各自需要两根火柴棍
2.如果A≠BA≠B,则A+B=CA+B=C与B+A=CB+A=C视为不同的等式(A,B,C>=0A,B,C>=0)
3.n根火柴棍必须全部用上
·思想:
枚举过样例,暴力出奇迹!
·疑难点:
暴力查找的上限是多少?
根据各个大佬(的题解)分析可得:上限为1111;
其中有一个涩会的大佬是那么说的:
"由于1111+1=1112已经用了25根小棒,已经超过了题目24根小棒的数据范围,所以可以直接枚举相加的两个数,上界为1111,再用拆数的方法求出这组要用的火柴棒根数,再计算次数就可以了。”
·代码:
#include<iostream> using namespace std; int n,ans=0; int a[10]={6,2,5,5,4,5,6,3,7,6};//存储0-9各需的火柴棒个数 int match(int x) { if(x==0) return 6;//因为0不能跳入while循环,所以特判 int tot=0; while(x>=1) { tot+=a[x-(x/10)*10];//算出每位数所需的火柴棒个数 x=x/10; } return tot;//此数所用的火柴棒个数 } int main() { cin>>n; int t=n-4; for(int i=0;i<=1111;i++) for(int j=0;j<=1111;j++) if(match(i)+match(j)+match(i+j)==t) ans++;//这个暴力就好了 cout<<ans<<endl; return 0; }