首页 > 试题广场 >

化冰

[编程题]化冰
  • 热度指数:3242 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

众所周知水在低于 0 摄氏度的时候是固态,在高于 0 摄氏度的时候是液态,在等于 0 摄氏度的时候是可能是液态也可能是固态还可能是固液混合

现在有个加热工具,加热 c 秒可以使冰升温 1 摄氏度。

加热 d 秒可以使 0 摄氏度的冰变成 0 摄氏度的水。

加热 e 秒可以使水升温 1 摄氏度。

现在给出两个温度 a , b ,请计算用这个加热工具将水从 a 摄氏度加热到 b 摄氏度所需要的时间。
保证a不为零。

数据范围:
进阶:空间复杂度 ,时间复杂度

输入描述:

输入数据共  行

第一行给出一个整数 

第一行给出一个整数 

第一行给出一个整数 

第一行给出一个整数 

第一行给出一个整数 




输出描述:
在一行中输出加热所需的时间
示例1

输入

-10
20
5
10
3

输出

120

说明

从-10摄氏度到0度需要10*5=50s
0摄氏度的冰加热到0摄氏度的水需要10s
0度的水加热到20摄氏度需要20*3=60s
50+10+60=120
示例2

输入

35
92
31
50
11

输出

627

说明

将水从35摄氏度加热到92摄氏度需要627s
const readline = require('readline');
let ans = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let K = 5;
let ipt = [];
let result = 0;
ans.on('line', function (data) {
    ipt.push(data.trim());
    for (let i = 0; i < ipt.length; i++) {
        ipt[i] = parseInt(ipt[i]);
    }
    if (ipt.length == K) {
        if (ipt[0] <= 0 && ipt[1] >= 0) {
            result = ipt[2] * (0 - ipt[0]) + ipt[3] + ipt[4] * (ipt[1] - 0);
        } else if (ipt[0] < 0 && ipt[1] < 0) {
            result = (ipt[1] - (ipt[0])) * ipt[2];
        } else if (ipt[0] > 0 && ipt[1] > 0) {
            result = (ipt[1] - (ipt[0])) * ipt[4];
        }
        console.log(result);
    }
});

😀JavaScript写的
发表于 2021-09-13 23:38:09 回复(0)
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
if A < 0:
    if B <= 0:
        t = C*(B-A)
    else:
        t = C*abs(A)+D+E*B
else:
    t = E*(B-A)
print(t)
发表于 2021-09-21 10:58:31 回复(1)
import java.util.*;
public class Main{
    public static void main(String args[]){
        int A,B,C,D,E;
        Scanner scan=new Scanner(System.in);
        A=scan.nextInt();
        B=scan.nextInt();
        C=scan.nextInt();
        D=scan.nextInt();
        E=scan.nextInt();
        if (A<0){
            if (B>0)
                 System.out.print(B*E+D+C*(-A));
            else System.out.print(C*(B-A));
        }
        else{
                 System.out.print((B-A)*E);
        }
         
    }
}

发表于 2021-09-15 13:46:24 回复(1)
用数学方法做就能达成题中进阶的复杂度,需要写成分段函数。
  1. 如果a>0,就不会存在从0下温度加热至0°的过程,耗时0秒;如果a<0,从a°升温至0°就需要耗费c*a秒。
  2. 如果a<0,就需要花费d秒将0°的冰变成0°的水;如果a>0,就没有这个过程,耗时0秒。
  3. 如果a<0,就需要花费d*b秒来将0°的水升温至b°;如果a>0,就只需要花费(b-a)*d秒将a°的水升温至b°。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());
        int d = Integer.parseInt(br.readLine());
        int e = Integer.parseInt(br.readLine());
        System.out.println(c*Math.max(0, -a) + (a < 0? d: 0) + e*(b - Math.max(0, a)));
    }
}

发表于 2022-01-16 22:43:26 回复(0)
a=[]
for i in range(5):
    a.append(int(input()))
if a[0]<0:
    b=-a[0]*a[2]+a[3]+a[1]*a[4]
else:
    b=a[4]*(a[1]-a[0])
print(b)

发表于 2021-10-31 22:13:34 回复(0)
一下代码,仅供参考
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,e;
    cin >> a ;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >>e;
    if (a > b || (a >-1 && a < 1)|| b > 100 || c>100 || d > 100 || e>100 )
    {
        return false;
    }
    if (a < 0 && b< 0)
    {
        cout << (b-a) * c << endl;
    }
    if (a < 0 && b >=0)
    {
        cout << (-a*c) + d + (b * e)<<endl;
    }
      
    if (a == b )
    {
        cout << '0' << endl;
    }
    if (a > 0 && b >0)
    {
        cout << (b-a)*e << endl;
    }
    return 0;
}


发表于 2021-09-13 13:17:25 回复(0)
 Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int e = sc.nextInt();
        if (a < 0 && b < 0) {
            System.out.println((b - a) * c);
        } else if (a < 0 && b > 0) { 
            System.out.println(-a * c + d + b * e);
        } else {
            System.out.println((b - a) * e);
        }

发表于 2023-05-10 11:00:09 回复(0)
import sys
 
a = int(sys.stdin.readline().strip())
b = int(sys.stdin.readline().strip())
c = int(sys.stdin.readline().strip())
d = int(sys.stdin.readline().strip())
e = int(sys.stdin.readline().strip())
 
if a < 0:
    print(abs(a)*c+d+b*e)
elif a==b:
    print(0)
else:
    print((b-a)*e)

发表于 2022-09-11 22:52:17 回复(0)
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
t = 0 if A < 0:
    t = -A * C + D + B * E else:
    t = (B - A) * E print(t)

编辑于 2022-04-20 14:11:47 回复(0)
a,b,c,d,e = int(input()),int(input()),int(input()),int(input()),int(input())
if a<0:
    a=-a
else:
    d=0
    b = b-a
    a=0
print(a*c+d+b*e)

发表于 2022-03-24 17:53:43 回复(0)
a=eval(input())
b=eval(input())
c=eval(input())
d=eval(input())
e=eval(input())
ifa<0:
    time=-a*c+d+b*e
elifa>0:
    time=(b-a)*e
print(time)
发表于 2021-11-11 11:06:27 回复(0)
import java.util.*;
public clas***ain{
    public static void main(String[] args){        
        Scanner s =  new  Scanner(System.in);
        int a,b,c,d,e;
        a = s.nextInt();
        b = s.nextInt();
        c = s.nextInt();
        d = s.nextInt();
        e = s.nextInt();
        int count = 0;
        if(a<0){
                count = Math.abs(a)*c+d+b*e;
        }else
            count = (b-a)*e;
        System.out.print(count);
        
    }
}
编辑于 2021-11-10 11:14:18 回复(0)
#include <iostream>
using namespace std;

int main() {
    int A,B,C,D,E;
    int ans;
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &C);
    scanf("%d", &D);
    scanf("%d", &E);
    if (A > 0) {
        ans = E * (B - A);
    } else {
        if (B < 0) {
            ans = C * (B - A);
        } else {
            ans = C * (0 - A) + D + E * (B - 0);
        }
    }
    printf("%d", ans);
    return 0;
}

发表于 2021-09-25 13:56:41 回复(0)
usingnamespacestd;
voidcal(intA,intB,intC,intD, intE){
    intls;
    if(A < 0){
        ls = -A*C +D+B*E;
    }
    if(A>0){
        ls = (B-A)*E;
    }
    if(A==0)
    {
        ls = D+B*E;
    }
    cout<<ls<<endl;
}
intmain(){
    cal(-10,20,5,10,3);
}
发表于 2021-09-12 18:03:09 回复(2)