Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1192

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1192

Paula's Mathematic Game

 

By Neilor Tonin, URI Brazil

Timelimit: 2

Paula loves math. Her main hobby is to invent games or activities to play with her friends. Obviously, not all of them are so passionated about mathematics and have a lot of difficulty to solve the games offered by her. Now Paula has invented a small hobby that involves three characters: a numerical digit, one letter and one numeric digit. 
If the letter is uppercase, you need to subtract the first digit of the second one. If the letter is lowercase, both digts must be added. If the DIGITS are the same, the product between these two digits must be presented. She asked his friend Marcelo, who is good at programming, to create a program that prints the solution for each sequence created by Paula.

 

Input

 

The input contains many test cases. The first line of each input contains a single integer N, indicating the number of following test cases. Each test case is a sequence of three chars created by Paula. This sequence contains in the first position a character between '0' and '9', a uppercase or lowercase character and another character between '0' and '9'.

 

Output

 

For each test case, one line must be printed with an integer, representing the solution for each sequence created by Paula

 

 

 

Input Sample Output Sample

5
4A5
3A3
4f2
2G4
7Z1

1
9
6
2
-6

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>

int upper(char x) {
    int i;
    char m[] = "QWERTYUIOPASDFGHJKLZXCVBNM";
    for (i = 0; i  <  26; ++i)
        if (x == m[i]) return 1;
    return 0;
}

int main(void) {
    int n, i, a, b;
    char e[4];
    scanf("%d", &n);
    for (i = 0; i  <  n; ++i) {
        scanf("%s", &e);
        a = e[0] - '0';
        b = e[2] - '0';
        if (a == b)
            printf("%d\n", a * b);
        else if (upper(e[1]))
            printf("%d\n", b - a);
        else
            printf("%d\n", a + b);
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
4A5
3A3
4f2
2G4
7Z1

Output

x
+
cmd
1
9
6
2
-6

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

bool upper(char x) {
    int i;
    char m[] = "QWERTYUIOPASDFGHJKLZXCVBNM";
    for (i = 0; i  <  26; ++i)
        if (x == m[i]) return true;
    return false;
}

int main(void) {
    int n, i, a, b;
    char e[4];
    cin >> n;
    for (i = 0; i  <  n; ++i) {
        cin >> e;
        a = e[0] - '0';
        b = e[2] - '0';
        if (a == b)
            cout << a * b << endl;
        else if (upper(e[1]))
            cout << b - a << endl;
        else
            cout << a + b << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
4A5
3A3
4f2
2G4
7Z1

Output

x
+
cmd
1
9
6
2
-6

#3 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
       Scanner leitor = new Scanner(System.in);
		int N = leitor.nextInt();
		for (int i = 0; i  <  N; i++) {
			String linha = leitor.next();
			int x = Character.getNumericValue(linha.charAt(0));
			char z = linha.charAt(1);
			int y = Character.getNumericValue(linha.charAt(2));
			if (x == y) {
				System.out.println(x * y);
			} else if (Character.isUpperCase(z)) {
				System.out.println(y - x);
			} else {
				System.out.println(x + y);
			}
		}
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
5
4A5
3A3
4f2
2G4
7Z1

Output

x
+
cmd
1
9
6
2
-6

#4 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
const n = Number(lines[0]);
for (let i = 1; i  < = n; i += 1) {
  let [a, l, b] = lines[i].split('');
  (a = Number(a)), (b = Number(b));

  if (a === b) {
    console.log(b * a);
  } else if (l.toUpperCase() === l) {
    console.log(b - a);
  } else {
    console.log(a + b);
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
4A5
3A3
4f2
2G4
7Z1

Output

x
+
cmd
1
9
6
2
-6

#5 Code Example with Python Programming

Code - Python Programming


q = int(input())
for p in range(q):
    e = str(input())
    d1 = int(e[0])
    d2 = int(e[2])
    if d1 == d2: print(d1 * d2)
    elif e[1].isupper(): print(d2 - d1)
    else: print(d1 + d2)
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
4A5
3A3
4f2
2G4
7Z1

Output

x
+
cmd
1
9
6
2
-6
Advertisements

Demonstration


Previous
#1190 Beecrowd Online Judge Solution 1190 Right Area Solution in C++, Java, Js and Python
Next
#1195 Beecrowd Online Judge Solution 11195 Binary Search Tree - Solution in C, C++, Java, Python and C#