ECNU 3416 摄氏华氏转换

ECNU 3416 摄氏华氏转换

链接

https://acm.ecnu.edu.cn/problem/3416

题目

单点时限: 0.5 sec

内存限制: 256 MB

输入 xxf 或 xxc,表示华氏温度或摄氏温度,输出对应的摄氏温度或华氏温度值,输出保留两位小数。

整数保证是介于 和 之间(闭区间)的两位数。整数值 xx 与 f/c 之间无空格。

样例
input
50f
output
10.00c

input
10c
output
50.00f

思路

具体公式没复制过来,直接看代码吧。
输入只有三位长的字符串,截取前两位是数字,第三位是f或c,分开计算。
结果控制一下位数即可。

代码

  public static void fun() {
    Scanner sc = new Scanner(System.in);
    String str = sc.next();
    DecimalFormat df = new DecimalFormat("0.00");
    int count = Integer.valueOf(str.substring(0, 2));
    if (str.charAt(2) == 'f') {
      double temp = count;
      System.out.print(df.format((((temp - 32) * 5) / 9)) + "c");
    } else {
      double temp = count;
      System.out.print(df.format((temp * 9) / 5 + 32) + "f");
    }

  }
原文地址:https://www.cnblogs.com/blogxjc/p/14295392.html