Algorithm
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1116
Dividing X by Y
Adapted by Neilor Tonin, URI Brazil
Write a program that read two numbers X and Y and print the result of dividing the X by Y. If it's not possible, print the message "divisao impossivel".
Input
The input contains an integer number N. This N is the quantity of pairs of integer numbers X and Y read (dividend and divisor).
Output
For each test case print the result of this division with one digit after the decimal point, or “divisao impossivel” if it isn't possible to perform the calculation.
Obs.: Be carefull. The division between two integers in some languages generates another integer. Use cast:)
Input Sample | Output Sample |
3 |
-1.5 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(1) << (x)
using namespace std;
int main(){
int n;
cin >> n;
for (int i = 0; i < n; i ++){
int x, y;
cin >> x >> y;
if (y == 0){
cout << "divisao impossivel" << endl;
}
else{
cout << FIXED_FLOAT(((1.0) * x) / y) << endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
import java.io.IOException;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i ++){
int x = input.nextInt();
int y = input.nextInt();
if (y == 0){
System.out.println("divisao impossivel");
}
else{
System.out.printf("%.1f\n", (((1.0) * x) / y));
}
}
}
}
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 sep = [' ', '\n'];
// console.log(separators.join('|'));
var line = input.split(new RegExp(sep.join('|'), 'g'));
var n = parseInt(line.shift());
for (var i = 0; i < n; i ++){
var x = parseInt(line.shift());
var y = parseInt(line.shift());
if (y === 0){
console.log("divisao impossivel");
}
else{
console.log((x / y).toFixed(1));
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
n = int(input())
for i in range(n):
x, y = map(int, input().split(" "))
if (y == 0):
print("divisao impossivel")
else:
print("{:.1f}".format(x / y))
Copy The Code &
Try With Live Editor
Input
Output