现在的位置: 首页 > 综合 > 正文

POJ_3299

2018年04月13日 ⁄ 综合 ⁄ 共 4514字 ⁄ 字号 评论关闭

一.题目

Description

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 + 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.

Sample Input

T 30 D 15
T 30.0 D 25.0
E

Sample Output

T 30.0 D 15.0 H 34.0
T 30.0 D 25.0 H 42.3




二.解题技巧

     本体主要是按照公式计算数值,并没有很多技巧在里面,唯一有挑战的就是那个小数的精度输出,不过,这个精度输出网上已经有很多方法可以控制,通过设置标志位std::fixed和输出精度precision可以控制输出的小数的小数点之后的位数,代码如下:
    cout << std::fixed ;  //设置标志位

    cout.precision(1);    //设置输出精度为小数点后1位



三.实现代码

#include <iostream>
using namespace std;

#include <cmath>
#include <stdio.h>


double CalEFromD(double Dew)
{
    double tmp = exp(5417.7530 * ((1.0 / 273.16)  - (1.0 / (Dew + 273.16))));
    tmp = 6.11 * tmp;

    return tmp;
}


double CalEFrowH(double H)
{
    double E = H / 0.5555 + 10;
    return E;
}


double CalHFromE(double E)
{
    double H = 0.5555 * (E - 10.0);
    return H;
}

double CalHFromTemAndHum(double Tem, double Hum)
{
    double H = Hum - Tem;
    return H;
}



double CalHumFromTemandDew(double Tem, double Dew)
{
    double E = CalEFromD(Dew);
    double H = CalHFromE(E);
    double Hum = Tem + H;

    return Hum;
}


double CalTemFrowDewandHum(double Dew, double Hum)
{
    double E = CalEFromD(Dew);
    double H = CalHFromE(E);
    double Tem = Hum - H;

    return Tem;
}


double CalDewFrowTemandHum(double Tem, double Hum)
{
    double H = Hum - Tem;
    double E = CalEFrowH(H);
    double tmp = E / 6.11;
    tmp = log(tmp);
    tmp = tmp / 5417.7530;
    tmp = 1 / 273.16 - tmp;
    tmp = 1 / tmp;
    double Dew = tmp - 273.16;

    return Dew;
}





void Output(double Tem, double Dew, double Hum)
{
    cout << "T " << Tem << " D " << Dew << " H " << Hum << endl;
}


const int TEM = 1;
const int DEW = 2;
const int HUM = 4;
const int TEMDEW = TEM | DEW;
const int TEMHUM = TEM | HUM;
const int DEWHUM = DEW | HUM;


void Calc(double& Tem, double& Dew, double& Hum, int& Mode)
{
    switch (Mode)
    {
    case TEMDEW:
        Hum = CalHumFromTemandDew(Tem, Dew);
        Output(Tem, Dew, Hum);
        Mode = 0;
        break;

    case TEMHUM:
        Dew = CalDewFrowTemandHum(Tem, Hum);
        Output(Tem, Dew, Hum);
        Mode = 0;
        break;

    case DEWHUM:
        Tem = CalTemFrowDewandHum(Dew, Hum);
        Output(Tem, Dew, Hum);
        Mode = 0;
        break;

    default:
        break;
    }
}



int main()
{

    bool IsWorks = true;
    int Mode = 0;
    double Tem = 0.0;
    double Dew = 0.0;
    double Hum = 0.0;
    char Type = 'A';

    cout << std::fixed ;
    cout.precision(1);

    while (IsWorks)
    {
        cin >> Type;
        switch (Type)
        {
        case 'T':
            cin >> Tem;
            Mode = Mode | TEM;
            Calc(Tem, Dew, Hum, Mode);
            break;

        case 'D':
            cin >> Dew;
            Mode = Mode | DEW;
            Calc(Tem, Dew, Hum, Mode);
            break;

        case 'H':
            cin >> Hum;
            Mode = Mode | HUM;
            Calc(Tem, Dew, Hum, Mode);
            break;

        case 'E':
            IsWorks = false;
            break;

        default:
            break;
        }
    }

    return 0;
}


四.体会

    1.不能将问题复杂化,例如本题虽然要求输出的小数要是小数点后一位,同时要求是最接近的一位,但是在实现的过程中,仅仅是将后面的精度抛弃而已,并不需要进行处理,在之前,我一直纠结于那个最接近的问题,太钻牛角尖了,这个习惯不好,要改。
    2.对于按照公式的实现过程,一定要认真检查公式的实现是否正确,是否输错常量或者计算符了,这次就是公式实现的过程中,将0.5555输入为0.55了,结果一直没有得到正确的结果。


版权所有,欢迎转载,转载请注明出处,谢谢微笑



【上篇】
【下篇】

抱歉!评论已关闭.