Poj3299 Humidex java 分析

来源:互联网 发布:解压缩软件64位 编辑:程序博客网 时间:2024/06/06 12:44

这题主要是考研 枚举 的思路, 读清题目, 找到关键要枚举的内容十分重要, 在第一次写的时候忽略了有“H”的可能,好在代码量不大,后期修改很方便。 题目中提到的数据的范围[-100, 100], 所以就可以用设置超值过滤的方式去筛选。 结合枚举的可能实例,公式要发生变化,可以写一行,可以分开写, 题目对内存占用还是放的很宽的,所以可以想定义几个变量就定义几个。 这里可以不用考虑先除减再加乘,和除0的可能。值得注意的是,题目中给定的e, 是自然数,对于需要用到的求导可以用Math.log()直接求出, 从而省略了用 log(result) / log(base).  OJ 支持java 1.5 所以switch 不能识别String.


Humidex
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19726 Accepted: 7139

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 + 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.0T 30.0 D 25.0 H 42.3

import java.util.Scanner;public class Poj3299{    public static void main(String[] args)    {Scanner scan = new Scanner(System.in);double t = 0.0, d = 0.0, h = 0.0;final double exp = 2.718281828;final double max = 101;int counter = 0;while(!scan.hasNext("E")){    counter = 0;    t = max;    d = max;    h = max;    while(true)    {if(counter == 2) break;switch(scan.next().toCharArray()[0]){case 'T':    t = scan.nextDouble();    break;case 'D':    d = scan.nextDouble();    break;case 'H':    h = scan.nextDouble();    break;}counter++;    }    if(h == 101)h = 0.5555 * (6.11 * Math.pow(exp, (5417.7530 * ((1.0 / 273.16) - (1.0 / (d + 273.16))))) - 10.0) + t;    else if(t == 101)t = h - 0.5555 * (6.11 * Math.pow(exp, (5417.7530 * ((1.0 / 273.16) - (1.0 / (d + 273.16))))) - 10.0);    else d = 1.0 / (1.0 / 273.16 - (Math.log(((h - t) / 0.5555 + 10.0) / 6.11) / 5417.7530)) - 273.16;    System.out.printf("T %.1f D %.1f H %.1f \n", t, d, h);}    }}



0 1
原创粉丝点击