简单的数学一元方程求解问题,分情况讨论即可.
题目中给出的是3个等式,最好将其中可以直接计算的部分算出,并且将其合并为一个方程,这样不仅可以减少运行中用于相同运算的时间,还有利于更好的求解.
不过,该题的一个关键点还是输入输出的处理.在使用cout.precision()时才发现它不是设定小数位数的,而是设定有效数字的位数,纠正一个错误.
Source:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void TD();
void TH();
void DH();
//humidex = temperature -5.555+ 3.394105*exp[19.833625-5417.753/(dewpoint+273.16)]
//
//h =3.394105*e - 5.555
//e = exp [19.833625 - 5417.753/(dewpoint+273.16)]
double t,d,h,e;
int main()
{
char ch;
while(1)
{
ch=getchar();
switch(ch)
{
case 'T':cin>>t;
getchar();
ch=getchar();
switch(ch)
{
case 'D':cin>>d;
TD();
break;
case 'H':cin>>h;
TH();
break;
}
getchar();
break;
case 'D':cin>>d;
getchar();
ch=getchar();
switch(ch)
{
case 'T':cin>>t;
TD();
break;
case 'H':cin>>h;
DH();
break;
}
getchar();
break;
case 'H':cin>>h;
getchar();
ch=getchar();
switch(ch)
{
case 'T':cin>>t;
TH();
break;
case 'D':cin>>d;
DH();
break;
}
getchar();
break;
case 'E':return 0;
}
cout.precision(3);
cout.setf(ios::showpoint);
cout.fill('0');
//cout.width(4);
cout<<"T "<<setw(4)<<t<<" D "<<setw(4)<<d<<" H "<<setw(4)<<h<<endl;
}
return 0;
}
void TD()
{
e=3.394105*exp(19.833625-5417.75/(d+273.16));
h=t+e-5.555;
}
void TH()
{
d=5417.753/(19.833625-log((h-t+5.555)/3.394105))-273.16;
}
void DH()
{
e=3.394105*exp(19.833625-5417.75/(d+273.16));
t=h+5.555-e;
}