In: Computer Science
Please code in python or C++
Misty is fond of pokemons and likes to collect pokemon cards. She has P pokemon cards with her at present. She wants to have a particular number of cards on the Dth day. Her friend Ash is ready to help her to achieve the number. Ash will provide her N cards daily so that a day before the Dth day, she will have the required number of Pokemon cards.
Example:
Misty has cards with her, P = 5
D = 6
Ash will provide her with cards daily, N = 4
Ash provides pokemon cards:
Day 1 = 4
Day 2 = 4
Day 3 = 4
Day 4 = 4
Day 5 = 4
Total cards Ash provides = 4 + 4 + 4 + 4 + 4 = 20
Total number of pokemon cards Misty has on the Dth day = 5 + 20 = 25
Misty is busy with her tournament and wants to know the total number of pokemon cards she will have on Dth day. Can you tell her?
Input Format
The first line of input consists of the number of test cases, T
The only line of each test case consists of three space-separated integers, P, N and D.
Constraints
1<= T <=100
1<= D <=100000
0 <= P, N <=100000
We have, initial cards = P.
Number of days for which cards are given = (D-1), since no cards are given on the Dth day. (e.g. as in the example above, D = 6, but the cards are given out only for 5 days).
Cards given per day = N.
Then, total number of cards at the end of the Nth day: P + (D-1)*N
Python code for the above problem:
T = int(input())
while T:
P, D, N = input().split()
P = int(P)
D = int(D)
N = int(N)
totalcards = P + (D-1)*N
print(totalcards)
T = T-1
We take the number of testcases as input, and then we use a while loop to take inputs for each testcase. We take P, D, N as space separated (default separator in the split() function is the space) values. Then, since they are positive integers, we convert them to integer inputs (since the default input type in python is a string). Once that is done, we use the formula derived above to calculate the total number of cards and we print it. We keep on iterating over each testcase and printing the values out.
Output:
You may also append the value of totalcards to a list for each testcase if so desired, but the output format was not specified in the question. The print() statement is the easiest method in this case.
(If you liked this answer, feel free to give it a thumbs up. Thank you so much!)