Algorithm
Problem Name: beecrowd | 2235
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2235
Walking in Time
By Maratona de Programação SBC Brazil
Timelimit: 1
Imagine you have a time machine that can be used up to three times, and every use of the machine you can choose to go back to the past or go to the future. The machine has three fixed loans; each credit represents a certain number of years, and can be used to get this amount of years for the past or for the future. You can make one, two or three trips, and each of these three credits can only be used once. For example, if the credits are 5, 12 and 9, you could decide to make two trips: go five years into the future and then go back nine years to the past. This way, you end four years in the past, in 2012. It could also make three trips, all going to the future, using the credits in any order, ending in 2042.
In this problem, given the values of the three machine credits, your program should say if is possible or not travel back in time and return to the present, making at least one trip and at most three trips; always using each of the three credit at most once.
Input
The input consists of a single line containing three credits A, B e C (1 ≤ A, B, C ≤ 1000).
Output
Your program should print a line containing the character 'S' if it possible travel in time and back to present, or 'N' if it's not possible.
Input Examples | Output Examples |
22 5 22 |
S |
31 110 79 |
S |
45 8 7 |
N |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main(void)
{
int a, b, c;
scanf("%i %i %i", &a, &b, &c);
if (a==b || b==c || c==a)
printf("S\n");
else if (a+b==c || a+c==b || b+c==a)
printf("S\n");
else
printf("N\n");
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <ostream>
int main(){
int cr[4] = {},boole = 0;
std::cin >> cr[0] >> cr[1] >> cr[2];
//cr[0]
if (cr[0] - cr[1] == cr[3] or cr[0] - cr[2] == cr[3]){
boole = 1;
}
//cr[1]
else if (cr[1] - cr[0] == cr[3] or cr[1] - cr[2] == cr[3]){
boole = 1;
}
//cr[2]
else if (cr[2] - cr[0] == cr[3] or cr[2] - cr[1] == cr[3]){
boole = 1;
}
//cr[0]+-cr[1]
else if (cr[0]+cr[1] - cr[2] == cr[3]){
boole = 1;
}
//cr[0]+-cr[2]
else if (cr[0]+cr[2] - cr[1] == cr[3]){
boole = 1;
}
//cr[1]+-cr[2]
else if (cr[1]+cr[2] - cr[0] == cr[3]){
boole = 1;
}
//true or false
if (boole) {
std::cout << "S" << std::endl;
}else {
std::cout << "N" << std::endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
var credits = prompt().split(" ").map(Number);
var travel = "N";
if (
credits[0] == credits[1] ||
credits[0] == credits[2] ||
credits[1] == credits[2]
) {
travel = "S";
}
if (
credits[0] + credits[1] == credits[2] ||
credits[1] + credits[2] == credits[0] ||
credits[2] + credits[0] == credits[1]
) {
travel = "S";
}
console.log(travel);
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
a, b, c = input().split()
a = int(a)
b = int(b)
c = int(c)
if((a-b == 0) or (a-c == 0) or (b-c == 0)):
print('S')
else:
if((a+b-c == 0) or (b-a+c == 0) or (c-a+b == 0)):
print('S')
elif((a-b-c == 0) or (b-a-c == 0) or (c-a-b == 0)):
print('S')
else:
print('N')
Copy The Code &
Try With Live Editor
Input
Output