问题及代码:
/*
*copyright (c) 2014,烟台大学计算机学院
*all rights reserved.
*文 件 名 : 求墨水遮住的数字 .cpp
*作 者 :张 鹏
*完成日期 :2014年10月27号
*版 本 号 :v1.1
*
*问题描述 :有等式[※×(※3O※)]^2=8※※9,其中※处为一个数字,O为一个运算符号滴上了墨水无法辨认。请编程找出※是那些数字。
*输入描述 :无
*程序输出 :输出原等式。
*/
#include <iostream> //预处理指令。
#include <cmath> //使用数学符号。
using namespace std; //使用C++的命名空间 std。
int main() //函数首部。
{
double iFirst,iSecond,iThird,iFourth,iFifth,iCase=1; //声明六个变量iFirst,iSecond,iThird,iFourth,iFifth,iCase为实型。
cout<<"原式可能是:"<<endl; //输出汉字,使得界面更友好。
for (iFirst=0; iFirst<=9; iFirst++) //穷举第一个※数字的可能性,下同。
for (iSecond=0; iSecond<=9; iSecond++)
for (iThird=0; iThird<=9; iThird++)
for (iFourth=0; iFourth<=9; iFourth++)
for (iFifth=0; iFifth<=9; iFifth++)
{
if ((iFirst*(iSecond*10+3+iThird)*iFirst*(iSecond*10+3+iThird)==8000+iFourth*100+iFifth*10+9))
{
cout<<"情况"<<iCase++<<": [";
cout<<iFirst<<"×("<<iSecond<<"3+";
cout<<iThird<<")]^2=8"<<iFourth<<iFifth<<"9"<<endl; //当O为+的时候。
}
if ((iFirst*(iSecond*10+3-iThird)*iFirst*(iSecond*10+3-iThird)==8000+iFourth*100+iFifth*10+9))
{
cout<<"情况"<<iCase++<<": [";
cout<<iFirst<<"×("<<iSecond<<"3-";
cout<<iThird<<")]^2=8"<<iFourth<<iFifth<<"9"<<endl; //当O为-的时候。
}
if ((iFirst*((iSecond*10+3)*iThird)*iFirst*((iSecond*10+3)*iThird)==8000+iFourth*100+iFifth*10+9))
{
cout<<"情况"<<iCase++<<": [";
cout<<iFirst<<"×("<<iSecond<<"3×";
cout<<iThird<<")]^2=8"<<iFourth<<iFifth<<"9"<<endl; //当O为*的时候。
}
if ((iFirst*((iSecond*10+3)/iThird)*iFirst*((iSecond*10+3)/iThird)==8000+iFourth*100+iFifth*10+9))
{
cout<<"情况"<<iCase++<<": [";
cout<<iFirst<<"×("<<iSecond<<"3÷";
cout<<iThird<<")]^2=8"<<iFourth<<iFifth<<"9"<<endl; //当O为/的时候。
}
}
return 0; //执行retur语句,结束程序。
}
运行结果:
知识点总结:
当o为÷号时要定义为double不然数据溢出,跑不出来。
学习心得:
多亏了学长提醒才知道把int改double,看来编程之路上我还是一个小菜鸟。