题目
T
he humidex is a measurement used by Canadian meteorologists to reflect the combined effect of heat and humidity. It differs from the heat index used in the United States in using dew point rather than relative humidity.
When the temperature is 30°C (86°F) and the dew point is 15°C (59°F), the humidex is 34 (note that humidex is a dimensionless number, but that the number indicates an approximate temperature in C). If the temperature remains 30°C and the dew point rises to 25°C (77°F), the humidex rises to 42.3.
The humidex tends to be higher than the U.S. heat index at equal temperature and relative humidity.
The current formula for determining the humidex was developed by J.M. Masterton and F.A. Richardson of Canada's Atmospheric Environment Service in 1979.
According to the Meteorological Service of Canada, a humidex of at least 40 causes "great discomfort" and above 45 is "dangerous." When the humidex hits 54, heat stroke is imminent.
The record humidex in Canada occurred on June 20, 1953, when Windsor, Ontario hit 52.1. (The residents of Windsor would not have known this at the time, since the humidex had yet to be invented.) More recently, the humidex reached 50 on July 14, 1995 in both Windsor and Toronto.
The humidex formula is as follows:
humidex = temperature + h h = (0.5555)× (e - 10.0) e = 6.11 × exp [5417.7530 × ((1/273.16) - (1/(dewpoint+273.16)))]
where exp(x) is 2.718281828 raised to the exponent x.
While humidex is just a number, radio announcers often announce it as if it were the temperature, e.g. "It's 47 degrees out there ... [pause] .. with the humidex,". Sometimes weather reports give the temperature and dewpoint, or the temperature and humidex, but rarely do they report all three measurements. Write a program that, given any two of the measurements, will calculate the third.
You may assume that for all inputs, the temperature, dewpoint, and humidex are all between -100°C and 100°C.
Input
Input will consist of a number of lines. Each line except the last will consist of four items separated by spaces: a letter, a number, a second letter, and a second number. Each letter specifies the meaning of the number that follows it, and will be either T, indicating temperature, D, indicating dewpoint, or H, indicating humidex. The last line of input will consist of the single letter E.
Output
For each line of input except the last, produce one line of output. Each line of output should have the form:
T number D number H number
where the three numbers are replaced with the temperature, dewpoint, and humidex. Each value should be expressed rounded to the nearest tenth of a degree, with exactly one digit after the decimal point. All temperatures are in degrees celsius.
题目大意就是,H代表humidex,T代表temperature,D代表dewpoint,给出H,T,D三个中的任意两个,都要输出剩下的那一个
他们之间的转化公式就是
humidex = temperature + h
h = (0.5555)× (e - 10.0)
e = 6.11 × exp [5417.7530 × ((1/273.16) - (1/(dewpoint+273.16)))]
<cmath>包含函数exp(),如果求解D的话,反解第三个式子就行,log反解,也有log()函数在cmath头文件中
思路
我是直接分了六种情况。。。整个代码还是比较繁琐的,而且很容易不小心反解错
注意控制精度,以及不要不小心丢失小数部分,该double的double,float的float
代码
#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
char a,b;
double a1,b1,c1;
while(cin>>a&&a!='E')
{
cin>>a1>>b>>b1;
if((a=='T'&&b=='D')||(b=='T'&&a=='D'))
{
if(a=='T')
{
double e=6.11*exp (5417.7530 *((1/273.16) - (1/(b1+273.16))));
c1=a1+(0.5555*(e-10.0));
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<a1<<" D "<<b1<<" H "<<c1<<endl;
}
else
{
double e=6.11*exp(5417.7530 *((1/273.16) - (1/(a1+273.16)))) ;
c1=b1+(0.5555*(e-10.0));
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<b1<<" D "<<a1<<" H "<<c1<<endl;
}
}
if((a=='T'&&b=='H')||(a=='H'&&b=='T'))
{
if(a=='T')
{
double h=b1-a1;
double e=h/0.5555+10.0;
double c1=1/((1/273.16-log(e/6.11)/5417.7530))-273.16;
//double c1=1/(5417.7530 *(1/273.16)-log(e/6.11))-273.16;
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<a1<<" D "<<c1<<" H "<<b1<<endl;
}
else
{
double h=a1-b1;
double e=h/0.5555+10.0;
double c1=1/((1/273.16-log(e/6.11)/5417.7530))-273.16;
//double c1=1/(5417.7530 *(1/273.16)-log(e/6.11))-273.16;
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<b1<<" D "<<c1<<" H "<<a1<<endl;
}
}
if((a=='D'&&b=='H')||(a=='H'&&b=='D'))
{
if(a=='D')
{
double e=6.11*exp (5417.7530 *((1/273.16) - (1/(a1+273.16))));
c1=b1-(0.5555)*(e - 10.0);
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<c1<<" D "<<a1<<" H "<<b1<<endl;
}
else
{
double e=6.11*exp (5417.7530 *((1/273.16) - (1/(b1+273.16))));
c1=a1-(0.5555)*(e - 10.0);
cout<<setiosflags(ios::fixed)<<setprecision(1)<<"T "<<c1<<" D "<<b1<<" H "<<a1<<endl;
}
}
}
return 0;
}