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

编程入门 行业动态 更新时间:2024-10-27 10:23:20

题目描述:
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;
}

更多推荐

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

本文发布于:2023-06-14 09:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Power   Calculation   Consumption

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!