题目:
给你一个四舍五入到两位小数的非负浮点数 celsius
来表示温度,以 摄氏度(Celsius)为单位。
你需要将摄氏度转换为 开氏度(Kelvin)和 华氏度(Fahrenheit),并以数组 ans = [kelvin, fahrenheit]
的形式返回结果。
返回数组 ans
。与实际答案误差不超过 10-5
的会视为正确答案。
注意:
开氏度 = 摄氏度 + 273.15
华氏度 = 摄氏度 * 1.80 + 32.00
示例 1 :
输入:celsius = 36.50 输出:[309.65000,97.70000] 解释:36.50 摄氏度:转换为开氏度是 309.65 ,转换为华氏度是 97.70 。
示例 2 :
输入:celsius = 122.11 输出:[395.26000,251.79800] 解释:122.11 摄氏度:转换为开氏度是 395.26 ,转换为华氏度是 251.798 。
提示:
0 <= celsius <= 1000
我最开始的回答
double* tempreture(double Celsius){
double Kelvin;
double Fahrenheit;
Kelvin = Celsius + 273.15;
Fahrenheit = Celsius * 1.80 +32.00;
double* temp[2];
temp[0] = Kelvin;
temp[1] = Fahrenheit;
return temp;
}
出现了如下问题:
1.函数没有使用leetcode提供的名称导致编译错误
solution.c: In function ‘main’
Line 42: Char 23: warning: implicit declaration of function ‘convertTemperature’ [-Wimplicit-function-declaration] [solution.c]
45 | double* ret = convertTemperature(param_1, &ret_size);
| ^~~~~~~~~~~~~~~~~~
Line 42: Char 23: warning: initialization of ‘double *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] [solution.c]
/leetcode/user_code/runcode_1724849747.2896535_SsodLBoygL_interpret_task/prog_joined.c:45: error: undefined reference to 'convertTemperature'
collect2: error: ld returned 1 exit status
2.当修改为leetcode的初始模板后,未提供returnSize的值导致解答错误。
修正后解答如下
double* convertTemperature(double celsius, int* returnSize) {
double Kelvin;
double Fahrenheit;
Kelvin = celsius + 273.15;
Fahrenheit = celsius * 1.80 +32.00;
double* ans = malloc(2*sizeof(double));
ans[0] = Kelvin;
ans[1] = Fahrenheit;
*returnSize = 2;
return ans;
}