分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
看到
陈同学做的一道题,手痒痒,也做了一份,凑个热闹。
/*(编程题)下列乘法算式中:每个汉字代表1个数字(1~9)。相同的汉字代表相同的数字,不同的汉字代表不同的数字。 赛软件 × 比赛 ---------- = 软件比拼试编程确定使得整个算式成立的数字组合,如有多种情况,请给出所有可能的答案。【参考结果】465 * 14 = 6510*/#include<iostream>using namespace std;int main(){ int product; for(int sai=1;sai<=9;++sai)//赛 for(int ruan=1;ruan<=9;++ruan)//软 for(int jian=1;jian<=9;++jian)//件 for(int bi=1;bi<=9;++bi)//比 if(sai!=ruan&&ruan!=jian&&jian!=bi) { product=(sai*100+ruan*10+jian)*(bi*10+sai); if(product/1000==ruan&&(product%1000)/100==jian&&(product%100)/10==bi) cout<<(sai*100+ruan*10+jian)<<'*'<<(bi*10+sai)<<'='<<product<<endl; } return 0;}
这个题有些问题。事实上,“拼”可以是0时,参考结果是唯一结果。
“拼”不可以是0时,
if(product/1000==ruan&&(product%1000)/100==jian&&(product%100)/10==bi)
应该为
if(product/1000==ruan&&(product%1000)/100==jian&&(product%100)/10==bi&&product%10!=0)
这个程序的算法复杂度比陈同学的更好一些; 每一内层for中先加if,还能更好。
编程序是要讲效率的。
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)