Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2682
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2682
Fault Detector
By Emilio Wuerges, UFFS Brazil
Time limit: 1
There is a machine, that produces an increasing sequence of numbers. That is, every number of this sequence is larger than its predecessor.
However, this machine is starting to break. When it starts, everything is OK. However, after some time, it starts producing wrong results.
Your task is, whenever the machine produces the first wrong number or if it turns off, ignore all following results and produce the next smallest valid number.
Since we are just checking the machine, we cannot turn it off. We have to wait it turn it of on its own. That is, we must keep reading numbers until the machine turns of on its own.
Input
The input consists of 0 < N < 104 lines, and ends with EOF.
Each line consists of a single integer 0 < X < 230.
Output
A single line, with a single integer Y, the solution for the problem.
Input Sample | Output Sample |
1 |
42 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
unsigned numeroAtual = 1, numeroAnt = 0, numero = 0;
bool temfalha = false;
while (scanf("%u", &numeroAtual) != EOF)
{
if ((!temfalha) && (numeroAtual < numeroAnt))
{
temfalha = true;
numero = numeroAnt;
}
numeroAnt = numeroAtual;
}
if (temfalha)
printf("%u\n", numero + 1);
else
printf("%u\n", numeroAtual + 1);
}
Copy The Code &
Try With Live Editor
Input
2
41
5
2
1
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
int max=0;
bool wrong=false;
while(cin>>n)
{
if(n>max&&!wrong)max=n;
else
{
wrong=true;
}
}
cout<<max+1<<endl;
}
Copy The Code &
Try With Live Editor
Input
2
41
5
2
1
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
"use strict"
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n").map(line => Number.parseInt(line, 10))
input.pop() // Remove EOF
const error = input.find((value, index, arr) => value > arr[index + 1]) || input[input.length - 1]
console.log(error + 1)
Copy The Code &
Try With Live Editor
Input
2
41
5
2
1
Output
#4 Code Example with Python Programming
Code -
Python Programming
maior = 0
ant = 0
while True:
try:
n = int(input())
if ant > n and maior == 0:
maior = ant + 1
ant = n
except EOFError:break
if maior == 0:maior = ant + 1
print(maior)
Copy The Code &
Try With Live Editor
Input
2
41
5
2
1
Output