Editorial-Game of Maths |Encoding Junior November 21

Practice
Contest Link
Problem Link

Author: Srinjoy Pal
Tester: Abhisekh Paul ,Sayan Poddar
Editorialist: Srinjoy Pal

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic Math , Basic data types

PROBLEM:

Take a character and an integer as input and perform required operations and print the output after each query.

EXPLANATION:

After taking each query check the character part .
‘A’ add the number.
‘S’ subtract the number
‘D’ divide the number
‘M’ multiply the number

SOLUTION:

Setter’s Solution
C++

#include <bits/stdc++.h>
using namespace std;
int main(){
long long int t;
cin>>t;
while(t--)
{
    long long int n,r;
    cin>>n>>r;
    while(r--)
    {
       char c;
       long long int m;
       cin>>c;
       if(c=='A')
       {
          cin>>m;
          n+=m;
       }
       else if (c=='S')
       {
          cin>>m;
          n-=m;
       }
       else if (c=='M')
       {
          cin>>m;
          n*=m;
       }
       else if (c=='D')
       {
          cin>>m;
          n=n/m;
       }
       cout<<n<<endl;
    }
}
return 0;
}

Tester’s Solution
Python

for _ in range(int(input())):
a=[int(i) for i in input().split()]
c=a[0]
for _ in range(a[1]):
    b=[i for i in input().split()]
    if b[0]=="A":
        c+=int(b[1])
        print(c)
    elif b[0]=="S":
        c-=int(b[1])
        print(c)
    elif b[0]=="M":
        c*=int(b[1])
        print(c)
    elif b[0]=="D":
        c/=int(b[1])
        c=int(c)
        print(int(c))

Tester’s Solution
Java

//package myplace;
import java.util.Scanner;
class ecjnov {
public static void  main (String[] args) {
    Scanner sc=new Scanner(System.in);
    int t=Integer.parseInt(sc.nextLine());
    while(t>0)
    {
        String s[]=sc.nextLine().split(" ");
        int n=Integer.parseInt(s[0]);
        int r=Integer.parseInt(s[1]);
        for(int i=0;i<r;i++)
        {
        	String s1[]=sc.nextLine().split(" ");
            String c=s1[0];
            int m=Integer.parseInt(s1[1]);
            if(c.equals("A"))
            	n=m+n;
            else if(c.equals("S"))
            	n=n-m;
            else if(c.equals("M"))
            	n=m*n;
            else
            	n=n/m;
            System.out.println(n);
        }
        
        t--;
    }
}}
2 Likes