Algorithm
Problem Name: beecrowd | 2140
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2140
Two Bills
By Joao Marcos Salvanini Bellini de Moraes, IFSULDEMINAS Brazil
Timelimit: 1
Gilberto is a famous sfiha vendor. However, although everyone likes his sfihas, he can only give the change with two different bills, i.e., it's not always possible to get the right change. In order to make Gil's life easier, write a program for him to check whether it's possible to give the exact change using two different bills.
Available bills: 2, 5, 10, 20, 50 and 100.
Input
The input contains an integer N representing the buy price and then an integer M representing the price paid by the costumer (N < M ≤ 104). Read input until N = M = 0.
Output
Print "possible" if it's possible to give the exact change or "impossible" if it's not.
Input Sample | Output Sample |
11 23 |
possible |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include
int main()
{
int a,b,t,c,i;
int d[15]= {7,12,22,52,102,15,25,55,105,30,60,110,70,120,150};
while(1)
{
scanf("%d %d",&a,&b);
if(a==0 && b==0)break;
c=(b-a);
for(i = 0,t = 0; i < 15; i++)
{
if(d[i]==c)
{
t=1;
break;
}
}
if(t)printf("possible\n");
else
printf("impossible\n">;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
let money = [2, 5, 10, 20, 50, 100];
let cont = 0;
while(true){
let [a, b] = lines.shift().split(" ");
cont = 0;
if(a == 0 && b == 0){
break;
}
let troco = parseInt(b) - parseInt(a);
if(troco > 200 || troco < 1){
console.log("impossible");
}
else{
for(let i = 1; i < = money.length;i++){
for(let j = 0; j < money.length; j++){
if(parseInt(a) + (money[i - 1] + money[j]) == parseInt(b) ){
console.log("possible");
cont ++;
break;
}
}
if(cont != 0){
break;
}
}
if(cont == 0){
console.log("impossible">;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
notas = [2, 5, 10, 20, 50, 100]
while True:
n, m = input().split()
if(n == '0' and m == '0'):
break
n = int(n)
m = int(m)
m -= n
total = 0
for i in notas:
if(m == i*2):
print('possible')
total = 2
break
if total == 0:
for i in range(len(notas)-1, -1, -1):
if total == 3:
break
if m - notas[i] >= 0:
m -= notas[i]
total += 1
if (m == 0 and total == 2):
print('possible')
else:
print('impossible')
Copy The Code &
Try With Live Editor
Input
Output