Advertisement
grubcho

Batteries

Jul 3rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You are in a battery manufacturing plant. Your task is to stress test the latest batch of batteries for longevity.
  7. //You will receive an array of doubles on the console (first line, space-separated), indicating the capacities of the different //batteries in the batch (in mAh). On the next line, you will receive the usage per hour for each battery as an array of doubles //(second line, space-separated).
  8. //Next, you will receive the amount of hours you have to stress test each battery for (as an integer). Each of the batteries drains by //its capacity until either the testing hours are over, or the battery dies (reaches 0 capacity).
  9. //Print the status of all the batteries at the end of the testing session (in percentage). If any batteries die, along with the //percentage, print how many hours it took. The capacity and percentage are rounded to the 2nd decimal point.
  10.  
  11. namespace Batteries
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             double[] capacity = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
  18.             double[] usagePerHour = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
  19.             int hours = int.Parse(Console.ReadLine());
  20.  
  21.             for (int i = 0; i < capacity.Length; i++)
  22.             {
  23.                 double startCapacity = capacity[i];
  24.                 double usedPower = hours * usagePerHour[i];
  25.                 double remainingPercent = ((capacity[i] - usedPower) / capacity[i]) * 100.0;
  26.                 capacity[i] -= usedPower;
  27.                 if (capacity[i] > 0)
  28.                 {
  29.                     Console.WriteLine("Battery {0}: {1:f2} mAh ({2:f2})%", i + 1, capacity[i], Math.Round(remainingPercent, 2));
  30.                 }      
  31.                 else if(capacity[i] <=0)
  32.                 {
  33.                     double workHours = Math.Ceiling(startCapacity / usagePerHour[i]);
  34.                     Console.WriteLine("Battery {0}: dead (lasted {1} hours)", i+1, workHours);
  35.                 }
  36.             }            
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement