Algorithm
Problem Name: beecrowd | 1150
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1150
Exceeding Z
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Write a program that reads two integers: X and Z (Z must be read as many times as necessary, until a number greater than X is read). Count how many integers must be summed in sequence (starting at and including X) so that the sum exceeds Z the minimum possible and writes this number.
The input may have, for example, the numbers 21 21 15 30. In this case, the number 21 is assumed for X, The numbers 21 and 15 must be ignored because they are smaller or equal to X. The number 30 is within the specification (greater than X) and is valid. So, the final result must be 2 for this test case, because the sum (21 + 22) is bigger than 30.
Input
The input contains only integer values, one per line, which may be positive or negative. The first number is the value of X. The next line will contain Z. If Z does not meet the specification of the problem, it should be read again, as many times as necessary.
Output
Print a line with an integer number representing the among of integer numbers that must be summed.
Input Sample | Output Sample |
3 |
5 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main(){
int x, z;
cin >> x;
cin >> z;
while(z < = x){ cin >> z;}
int sum = 0;
int cnt = 1;
for (int i = x; i < z; i++){
sum += i;
if (sum < z){
cnt += 1;
}
}
cout << cnt << 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 x = input.nextInt();
int z = input.nextInt();
while(z < = x){ z = input.nextInt();}
int sum = 0;
int cnt = 1;
for (int i = x; i < z; i++){
sum += i;
if (sum < z){
cnt += 1;
}
}
System.out.println(cnt);
}
}
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 x = parseInt(lines.shift());
var z = parseInt(lines.shift());
while(z < = x){ z = parseInt(lines.shift());}
var sum = 0;
var cnt = 1;
for (var i = x; i < z; i++){
sum += i;
if (sum < z){
cnt += 1;
}
}
console.log(cnt);
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
x = int(input())
z = int(input())
while(z <= x):
z = int(input())
sum = 0
cnt = 1
for i in range(x, z):
sum += i
if (sum < z):
cnt += 1
print(cnt)
Copy The Code &
Try With Live Editor
Input
Output