Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The Prince has an initial energy of E. In each battle, the Prince will
loses as much energy as the opponent's. If the energy possessed
Prince becomes zero or minus, then it means Prince has been defeated (dead).


INPUT:
1. The first row is 2 integers E and N (separated by spaces). E is the energy that the Prince have, and N is the number of opponents the Prince will face.
2. The second row is whole number as much as N separated by spaces, the energy of each opponents the Prince will face.

OUTPUT:
1. The output is an integer indicating in which battle the Prince lost.

TEST CASE:
TEST CASE 1: 10 5
1 2 3 4 5
4 (output)
TEST CASE 2: 77 7
33 33 33 33 33 33 33
3 (output)
TEST CASE 3: 75 15
1 2 3 4 5 6 7 8 9 10 10 10 10 10 10
12 (output)
TEST CASE 4: 999 7
1000 1 1 1 1 1 1
1 (output)

What I have tried:

#include<bits stdc++.h="">
using namespace std;

int main()
{
int E, n, sum=0;
cin >> E >> n;

int arr[n];
for(int i=0; i<n; i++){
="" cin="">>arr[i];
}

for(int i=0; i
Posted
Updated 21-Nov-21 1:19am

1 solution

Loop though the enemy input values, keeping a count of the "round".
Each time round the loop, read an enemy energy from the input stream, and subtract it from the princes remaining energy.
If the result is less than or equal to zero, print the round number and exit the app.
Otherwise, increment the round number and go around again to read another enemy energy.

You don't need an array, you don't need a total. Just a loop and subtraction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900