//第七屆藍橋杯軟件類省賽真題-C-B-3_湊算式 /*題目: 湊算式A + B/C + DEF/GHI = 10(如果顯示有問題,可以參見【圖1.jpg】)這個算式中A~I代表1~9的數(shù)字,不同的字母代表不同的數(shù)字。比如: 6+8/3+952/714 就是一種解法, 5+3/1+972/486 是另一種解法。這個算式一共有多少種解法?注意:你提交應該是個整數(shù),不要填寫任何多余的內容或說明性文字。*//*【解題思路】解法一:暴力枚舉解法二:深度優(yōu)先搜索 答案:29*/#include<iostream>using namespace std;int main(){ double a,b,c,d,e,f,g,h,i; int count = 0; for(a = 1.0;a <= 9.0;a++) for(b = 1.0;b <= 9.0;b++) for(c = 1.0;c <= 9.0;c++) for(d = 1.0;d <= 9.0;d++) for(e = 1.0;e <= 9.0;e++) for(f = 1.0;f <= 9.0;f++) for(g = 1.0;g <= 9.0;g++) for(h = 1.0;h <= 9.0;h++) for(i = 1.0;i <= 9.0;i++){ if(a != b && a != c && a != d && a != e && a != f && a != g && a != h && a != i && b != c && b != d && b != e && b != f && b != g && b != h && b != i && c != d && c != e && c != f && c != g && c != h && c != i && d != e && d != f && d != g && d != h && d != i && e != f && e != g && e != h && e != i && f != g && f != h && f != i && g != h && g != i && h != i){ if(a+b/c+(d*100+e*10+f)/(g*100+h*10+i) == 10.0){// cout<<"a="<<a<<" b="<<b<<" c="<<c<<" d="<<d<<" e="<<e<<" f="<<f// <<" g="<<g<<" h="<<h<<" i="<<i<<endl; count++; } } } cout<<"這個算式共有的解法種數(shù)為:"<<count<<endl; return 0;}/*#include<iostream>using namespace std;double a[9];bool visit[9];int count = 0;void dfs(int step){ if(step == 9) { if(a[0]+a[1]/a[2]+(a[3]*100+a[4]*10+a[5])/(a[6]*100+a[7]*10+a[8]) == 10.0){ count++; } return; } for(int i = 0;i < 9;i ++) { if(visit[i] == false) { a[step] = i+1.0; visit[i] = true; dfs(step+1); visit[i] = false; } } return;}int main(){ dfs(0); cout<<"這個算式共有的解法種數(shù)為:"<<count<<endl; return 0;}*/
新聞熱點
疑難解答