POJ 3299 Humidex

来源:互联网 发布:沃虎送货单软件 编辑:程序博客网 时间:2024/06/05 08:42

Adapted from Wikipedia, the free encyclopedia

The 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 + hh = (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.
Sample Input
T 30 D 15T 30.0 D 25.0E
Sample Output
T 30.0 D 15.0 H 34.0

T 30.0 D 25.0 H 42.3

本题本质上就是一道公式题,按题目所给要求算即可

我的代码,WA好几发。。。原因是在G++下 DOUBLE 要用%lf输入,要用%f来输出,不能用%lf输出。

#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>double humidex(double T,double D){double e,h;e=6.11*exp(5417.7530*((1/273.16) - (1/(D+273.16))));h=(0.5555)*(e-10.0);return T+h;} double temperature(double D,double H){double e,h;e=6.11*exp(5417.7530*((1/273.16) - (1/(D+273.16))));h=(0.5555)*(e-10.0);return H-h;}double dewpoint(double T,double H){double e,h,l;h=H-T;e=(h/0.5555)+10.0;l=((log(e/6.11))/5417.7530);return (273.16*273.16*l)/(1-273.16*l);}int main(){char first,second;double a,b,c;while(scanf("%c",&first)!=EOF){if(first=='E') break;scanf("%lf %c %lf",&a,&second,&b);if(first=='T'&&second=='D') {c=humidex(a,b); printf("T %.1f D %.1f H %.1f\n",a,b,c);}if(first=='D'&&second=='T') {c=humidex(b,a); printf("T %.1f D %.1f H %.1f\n",b,a,c);}if(first=='D'&&second=='H') {c=temperature(a,b); printf("T %.1f D %.1f H %.1f\n",c,a,b);}if(first=='H'&&second=='D') {c=temperature(b,a); printf("T %.1f D %.1f H %.1f\n",c,b,a);}if(first=='T'&&second=='H') {c=dewpoint(a,b); printf("T %.1f D %.1f H %.1f\n",a,c,b);}if(first=='H'&&second=='T') {c=dewpoint(b,a); printf("T %.1f D %.1f H %.1f\n",b,c,a);}}return 0;}



当然以上代码太繁琐,我找到了别人的代码。以后要学习这种编程方式,要简洁。


#include <iostream>#include<math.h>#include<iomanip> using namespace std;  int main() {        char alpha;      double t,d,h;      int i;  //输入变量计数        for(;;){          t=d=h=200;    //三个参数的默认范围为-100~100          for(i = 0; i<2 ;i++){              cin>>alpha;              if(alpha=='E')                  return 0;  //程序退出              else if(alpha=='T')                  cin>>t;              else if(alpha=='D')                 cin>>d;              else if(alpha=='H')                 cin>>h;        }                if(h==200)                    h=t+0.5555*(6.11*exp(5417.7530*(1/273.16-1/(d+273.16)))-10);                  else if(t==200)                     t=h-0.5555*(6.11*exp(5417.7530*(1/273.16-1/(d+273.16)))-10);                  else if(d==200)                       d=1/((1/273.16)-((log((((h-t)/0.5555)+10.0)/6.11))/5417.7530))-273.16;                  cout<<setprecision(1)<<fixed<<"T "<<t<<" D "<<d<<" H "<<h<<endl;                //setprecision(1)格式化输出      }    return 0;  }









0 0