POJ 3299 Humidex

题目:三个变量temperature,dewpoint,humidex有以下关系:

humidex = temperature + h
h = (0.5555)× (e - 10.0)
e = 6.11 × exp [5417.7530 × ((1/273.16) - (1/(dewpoint+273.16)))]

任务是根据其中任意两个数,计算出第三个数。

输入:输入的每一行由四项组成,第一项为一个字符,第二项为一个数字,第三项为一个字符,第四项为数字,各项之间用空格分开,字符指出了后面的数字代表哪个变量,‘T’代表temperature,‘D’代表dewpoint, ‘H’代表humidex。最后一行输入仅包含一个字符‘E’。

输出:针对每行的输入,输出三个变量,输出的格式为: T number D number H number。

解题思路:该题简单,主要是利用公式进行计算。

代码:

#include<stdio.h>
#include<math.h>

float getHumidex(float temperature, float dewpoint)
{
	float humidex,h,e;
	e=6.11*exp(5417.7530*((1/273.16)-(1/(dewpoint+273.16))));
	h=0.5555*(e-10.0);
	humidex=temperature+h;
	return humidex;
}

float getTemperature(float dewpoint, float humidex)
{
	float temperature,h,e;
	e=6.11*exp(5417.7530*((1/273.16)-(1/(dewpoint+273.16))));
	h=0.5555*(e-10.0);
	temperature=humidex-h;
	return temperature;
}

float getDewpoint(float temperature, float humidex)
{
	float dewpoint,e,h;
	h = humidex-temperature;
	e=h/0.5555+10.0;
	dewpoint=1/((1/273.16)-log(e/6.11)/5417.7530) - 273.16;
	return dewpoint;
}


void calculate(float *temperature, float *dewpoint, float *humidex, int digit)
{
	switch(digit)
	{
		case 3: //已知 dewpoint, humidex
		*temperature = getTemperature(*dewpoint, *humidex);
		break;
		case 5: //已知 temperature, humidex
		*dewpoint = getDewpoint(*temperature, *humidex);
		break; 
		case 6: //已知 temperature, dewpoint
		*humidex = getHumidex(*temperature, *dewpoint);
		break;
	}
}

int main()
{
	float temperature, dewpoint, humidex, temp;
	char ch[2];
	int count=0,digit=0;
	scanf("%s",ch);
	while(ch[0] != 'E')
	{
		scanf("%f", &temp);
		count = (count+1)%2;
		if(ch[0] == 'T')
		{
		    temperature=temp;
		    digit += 4;
		}
		else if(ch[0] == 'D')
		{
			dewpoint=temp;
			digit += 2;
		}
		else if(ch[0] == 'H')
		{
			humidex=temp;
			digit += 1;
		}
		if(count==0) //输入了两个数,开始计算第三个数 
		{
			calculate(&temperature, &dewpoint, &humidex, digit);
			digit=0;
			printf("T %.1f D %.1f H %.1f\n", temperature, dewpoint, humidex);
		}
		
		scanf("%s",ch);
	}
	
	
	return 0;
}




题目POJ 3299通常是关于排序或者查找算法的问题,由于没有提供具体的题目链接,我将以一个常见的例子,如二分查找算法(Binary Search)来举例,因为这个算法在处理有序数组时非常常见。 假设这是一道要求你在已排序整数数组中寻找目标值的问题,可以使用C语言编写: ```c #include <stdio.h> // 定义二分查找函数 int binary_search(int arr[], int left, int right, int target) { if (right >= left) { int mid = left + (right - left) / 2; // 如果中间元素等于目标值,返回其索引 if (arr[mid] == target) return mid; // 否则,如果目标值小于中间元素,缩小搜索范围到左半部分 else if (arr[mid] > target) return binary_search(arr, left, mid - 1, target); // 否则,搜索右半部分 else return binary_search(arr, mid + 1, right, target); } // 没有找到目标值,返回 -1 return -1; } int main() { int arr[] = {1, 3, 5, 7, 9}; // 示例数组 int n, target; scanf("%d", &n); // 数组长度 for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } scanf("%d", &target); // 目标值 int result = binary_search(arr, 0, n - 1, target); if (result != -1) printf("Element is present at index %d\n", result); else printf("Element is not present in array\n"); return 0; } ``` 解析步骤: 1. 定义 `binary_search` 函数,它接收四个参数:一个已排序的整数数组、数组的起始索引、结束索引和目标值。 2. 通过不断将搜索区间分成两半,直到找到目标值或区间为空。每次比较中间元素,如果相等,则返回索引;如果大于目标值,继续在左半区查找;反之,在右半区查找。 3. 主函数中,读取数组长度、数组元素和目标值,然后调用 `binary_search` 并打印结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值