Algorithm
Problem Name: beecrowd | 2334
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2334
Little Ducks
By Hiago Oliveira de Jesus, UEA Brazil
Timelimit: 1
Five little ducks went for a walk. Beyond the mountains. To play. Their mom cried: 'Quack, quack, quack, quack'. But only four little ducks came back from there. Four little ducks went for a walk. Beyond the mountains. To play. Their mom cried: 'Quack, quack, quack, quack'. But only three little ducks came back from there. Three little ducks went for a walk. Beyond the mountains. To play. Their mom cried: 'Quack, quack, quack, quack'. But only two little ducks came back from there. Two little ducks went for a walk. Beyond the mountains. To play. Their mom cried: 'Quack, quack, quack, quack'. But only one little duck came back from there. One little duck went for a walk. Beyond the mountains. To play. Its mom cried: 'Quack, quack, quack, quack'. But no little ducks came back from there.
The mother duck was so sad that day decided to ask your help to look beyond the mountains, at the seashore, how many ducklings did not return from there.
Input
There will be several test cases, the first line of each test case contains an integer (0 ≤ P ≤ 1019) representing the total amount of ducks, the input ends with P = -1.
Output
The output file should contain the amount of little ducks returned there.
Input Sample | Output Sample |
0 1 10000000000000000000 -1 |
0 0 9999999999999999999 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main(void)
{
unsigned long long int i, j, n;
while (1)
{
scanf("%llu", &n);
if (n==-1)
break;
if (n==0)
j=n;
else
j=n-1;
printf("%llu\n", j);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
unsigned long long n;
while(cin >> n)
{
if(n == -1) break;
if(n == 0) printf("0\n");
else printf("%llu\n", n - 1);
}
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();};
while (true) {
var ducks = prompt();
if (ducks < 0) {
break;
}
if (0 < ducks && ducks < 10000000000) {
ducks = (parseInt(ducks) - 1).toString();
} else if (0 < ducks) {
var half1 = ducks.slice(-10);
var half2 = ducks.slice(0, -10);
if (half1 > 0) {
half1 = (parseInt("1" + half1) - 1).toString();
half1 = half1.slice(-10);
} else {
half1 = "9999999999";
half2 = (parseInt(half2) - 1).toString();
}
ducks = half2 + half1;
}
console.log(ducks);
}
Copy The Code &
Try With Live Editor
Input
Output