Question 8

8 comments on “Question 8”


  • ismail.mohd0603

    In cpp
    #include
    #include
    using namespace std;
    int main(){
    int p,d,m,s;
    cin>>p>>d>>m>>s;
    int ans=20;
    int cnt=1;
    while(1){
    if(p-d>m) {
    ans+=(p-d);
    p=p-d;
    if(ans<s) cnt++;
    else break;
    }
    else{
    ans+=m;
    if(ans<s) cnt++;
    else break;
    }
    }
    if(s<p) cout<<0<<endl;
    else cout<<cnt<<endl;
    return 0;
    }


  • culbgaming12

    #include
    using namespace std;

    int main()
    {
    int p, d, m, s;

    cin >> p >> d >> m >> s;

    int sum = 0;
    int count = 0;
    while (true)
    {
    if (p <= m && sum m && sum s)
    {
    count–;
    break;
    }
    }

    cout << count << endl;

    return 0;
    }


  • abhitejkumarvoruganti

    #!/bin/python3

    import math
    import os
    import random
    import re
    import sys

    #
    # Complete the ‘howManyGames’ function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    # 1. INTEGER p
    # 2. INTEGER d
    # 3. INTEGER m
    # 4. INTEGER s
    #

    def howManyGames(p, d, m, s):
    a=p-m
    b=a//d
    c=a%d
    k=b+1
    #print(k,’k’)
    # if c!=0:
    # k=b+1
    # print(k,’k’)
    # print(b,c,’b’,’c’)
    # print(b*d,’b*d’)
    # print(p-b*d,’p-b*d’)
    # print((p+(p-b*d))/2,’p+(p-b*d))/2′)
    r=(b+1)*((p+(p-b*d))/2)
    #print(r,’r’)
    if r<=s:
    s=int(s-r)
    # print(s)
    # print('////////')

    c1=s%m
    #print(c1,'c1')
    b1=(s-c1)//m
    #print(b1,'b1')
    k=k+b1
    #print(k,'k')
    r1=(b1+1)*((s+(s-b1*d))//2)
    #print(r1,'r1')
    s=s-r1
    #print(s,'s')
    # while True:
    # print(s,d)
    # if sp):
    s=s-p
    q3+=1
    p=p-d
    print(q3)
    return q3
    return k
    # Return the number of games you can buy

    if __name__ == ‘__main__’:
    fptr = open(os.environ[‘OUTPUT_PATH’], ‘w’)

    first_multiple_input = input().rstrip().split()

    p = int(first_multiple_input[0])

    d = int(first_multiple_input[1])

    m = int(first_multiple_input[2])

    s = int(first_multiple_input[3])

    answer = howManyGames(p, d, m, s)

    fptr.write(str(answer) + ‘\n’)

    fptr.close()


  • rutuja123desai

    Java code:

    public static int howManyGames(int p, int d, int m, int s) {
    // Return the number of games you can buy
    if(s >= p)
    {
    int b = 1;
    s = s-p;
    while(s >= p-d && s >= m)
    {

    p = p-d;

    if(p < m)
    {
    s = s-m;
    }
    else
    {
    s = s-p;
    }
    b++;
    }
    return b;
    }
    else
    {
    return 0;
    }
    }

    }


  • Pratik

    def howManyGames(p,d,m,s):
    games = 0

    while p <= s:
    s = s-p
    p = max(p-d, m)
    games = games+1

    return games

    p,d,m,s = map(int, input.split())
    answer = howManyGames(p,d,m,s)
    print(answer)


  • Gangadhar

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Solution {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter p, d,m,s:”);
    int p = sc.nextInt();
    int d = sc.nextInt();
    int m = sc.nextInt();
    int s = sc.nextInt();
    sc.close();
    int games = numberOfGames(p,d,m,s);
    int games2= numberOfGames2(p,d,m,s);
    System.out.println(“No of games bought: “+games2);
    System.out.println(“No of games bought: “+games);
    }

    //2nd method
    private static int numberOfGames2(int p, int d, int m, int s) {
    int count = 0;
    int sum=0;
    while(sum=m){
    if(p>m){
    sum+=p;
    }else{
    sum+=m;
    }
    count++;
    p=p-d;

    }

    return count;
    }
    }


  • Gangadhar

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Solution {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter p, d,m,s:”);
    int p = sc.nextInt();
    int d = sc.nextInt();
    int m = sc.nextInt();
    int s = sc.nextInt();
    sc.close();
    int games = numberOfGames(p,d,m,s);
    int games2= numberOfGames2(p,d,m,s);
    System.out.println(“No of games bought: “+games2);
    System.out.println(“No of games bought: “+games);
    }

    //2nd method
    private static int numberOfGames2(int p, int d, int m, int s) {
    int count = 0;
    int sum=0;
    while(sum=m){
    if(p>m){
    sum+=p;
    }else{
    sum+=m;
    }
    count++;
    p=p-d;

    }

    return count;
    }

    //1st method
    private static int numberOfGames(int p, int d, int m, int s) {
    ArrayList list = new ArrayList();
    list.add(p);
    for (int i = 0; i
    m) {
    list.add(p);
    }else {
    list.add(m);
    }
    }

    int sum=0,count=0;
    for (int i = 0; i < list.size(); i++) {
    if (sum<s) {
    sum+=list.get(i);
    count++;
    }

    }
    return count-1;
    }

    }


  • Ria

    static void howmanyGames(int p,int d,int m,int s){
    int count = 0;
    int lastpurchase= 0;
    while(s >= m){
    if(s > p && count == 0){
    s = s – p;
    lastpurchase = p ;
    count++;
    System.out.println(“S:”+s+”||lastpurchase:”+lastpurchase+”||count:”+count);
    }
    if(lastpurchase > m){
    lastpurchase = lastpurchase – d;
    s = s – lastpurchase;
    count++;
    System.out.println(“S:”+s+”||lastpurchase:”+lastpurchase+”||count:”+count);
    }
    else{
    lastpurchase = m;
    s = s – lastpurchase;
    count++;
    System.out.println(“S:”+s+”||lastpurchase:”+lastpurchase+”||count:”+count);
    }

    }
    System.out.println(count);
    }