2805. Power Consumption Calculation耗电计算(c++)

题目描述:
Power Consumption Calculation
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the “sleep” mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom’s work with the laptop can be divided into n time periods [l1,r1],[l2,r2],…,[ln,rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1,rn].

Input
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1≤n≤100,0≤P1,P2,P3≤100,1≤T1,T2≤60). The following n lines contain description of Tom’s work. Each i-th of these lines contains two space-separated integers li and ri (0≤li<ri≤1440, ri<li+1 for i<n), which stand for the start and the end of the i-th period of work.

Output
Output the answer to the problem.

Examples
Input

1 3 2 1 5 10
0 10

Output

30

Input

2 8 4 2 5 10
20 30
50 100

Output

570

这道题就是要我们计算笔记本在[l1,rn]期间消耗的总电量。
首先输入n,表示笔记本电脑的工作分为n个时间段。然后依次输入P1、P2、P3、T1、T2,表示正常使用时耗电为P1;暂停使用T1分钟后,进入屏保模式,耗电为P2;屏保模式持续T2分钟后进入睡眠模式,耗电为P3。
代码思路如下:
使用一个二维数组记录输入的n个时间段,用一个变量记录耗电量,然后在循环语句中使用条件语句对不同时间段之间的间隔(用t表示)进行分析。有以下三种情况:
当间隔小于T1时(也就是没进入屏保模式)这期间耗电量为t✖P1;
当间隔大于T1小于T1+T2时(也就是进入屏保模式但没有进入睡眠模式)这期间耗电量为T1✖P1+(t-T1)✖P2;
当间隔大于T1+T2时(也就是进入睡眠模式)这期间耗电量为T1✖P1+T2✖P2+(t-T1-T2)✖P3;

#include<iostream>
using namespace std;
int main()
{
 int n,p1,p2,p3,t1,t2;
 cin>>n>>p1>>p2>>p3>>t1>>t2;
 int shi[n][2];//不能同时初始化赋值,否则会编译错误
 for(int i=0;i<n;i++)
 {
  cin>>shi[i][0]>>shi[i][1];//使用二维数组记录n个时间段
 }
 int sum=0;//用变量sum记录耗电量
 sum=(shi[0][1]-shi[0][0])*p1;//sum=第一个时间段的耗电量
 for(int i=1;i<n;i++)
 {
  sum+=(shi[i][1]-shi[i][0])*p1;
  int t=shi[i][0]-shi[i-1][1];//t为第i个时间段和第i-1个时间段之间的时间间隔
  if(t<=t1) sum+=t*p1;//没进入屏保模式,耗电量为t*P1;
  else if(t>t1&&t<(t1+t2)) sum+=t1*p1+(t-t1)*p2;
  //进入屏保模式但没有进入睡眠模式,耗电量为T1*P1+(t-T1)*P2;
  else if(t>=(t1+t2)) sum+=t1*p1+t2*p2+(t-t1-t2)*p3;
  //进入睡眠模式,耗电量为T1*P1+T2*P2+(t-T1-T2)*P3;
 }
 cout<<sum<<endl;
 return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值