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
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 |
1 |
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
4A5
3A3
4f2
2G4
7Z1
Output
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
4A5
3A3
4f2
2G4
7Z1
Output
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
4A5
3A3
4f2
2G4
7Z1
Output
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
4A5
3A3
4f2
2G4
7Z1
Output
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
4A5
3A3
4f2
2G4
7Z1
Output
9
6
2
-6