Algorithm


Problem Name: beecrowd | 1117

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1117

beecrowd | 1117

Score Validation

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

 

Write a program that reads two scores of a student. Calculate and print the average of these scores. Your program must accept just valid scores [0..10]. Each score must be validated separately.

Input

The input file contains many floating-point numbers​​, positive or negative. The program execution will be finished after the input of two valid scores.

Output

When an invalid score is read, you should print the message "nota invalida".
After the input of two valid scores, the message "media = " must be printed followed by the average of the student. The average must be printed with 2 numbers after the decimal point.

 
Input Sample Output Sample

-3.5
3.5
11.0
10.0

nota invalida
nota invalida
media = 6.75

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)

using namespace std;

int main(){
    int cnt = 0;
    float sum = 0;
    while(cnt != 2){
        float num;
        cin >> num;
        if (num >= 0 && num  < = 10){
            sum += num;
            cnt += 1;
        }
        else{
            cout << "nota invalida" << endl;
        }
    }
    cout << "media = " << FIXED_FLOAT(sum / cnt) << endl;
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0

Output

x
+
cmd
nota invalida nota invalida media = 6.75

#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 cnt = 0;
        float sum = 0;
        while(cnt != 2){
            float num = input.nextFloat();
            if (num >= 0 && num  < = 10){
                sum += num;
                cnt += 1;
            }
            else{
                System.out.println("nota invalida");
            }
        }
        System.out.printf("media = %.2f\n", (sum / cnt));
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0

Output

x
+
cmd
nota invalida nota invalida media = 6.75

#3 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var sum = 0;
var cnt = 0;

while(cnt != 2){
    var num = parseFloat(lines.shift());
    if (num >= 0 && num  < = 10){
        sum += num;
        cnt += 1;
    }
    else{
        console.log("nota invalida");
    }
}
console.log("media =", (sum / cnt).toFixed(2));
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0

Output

x
+
cmd
nota invalida nota invalida media = 6.75

#4 Code Example with Python Programming

Code - Python Programming


sum = 0
cnt = 0

while(cnt != 2):
    num = float(input())
    if (num >= 0 and num <= 10):
        sum += num
        cnt += 1
    else:
        print("nota invalida")

print("media = {:.2f}".format(sum / cnt))
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0

Output

x
+
cmd
nota invalida nota invalida media = 6.75
Advertisements

Demonstration


Previous
#1116 Beecrowd Online Judge Solution 1116 Dividing X by Y Solution in C++, Java, Js and Python
Next
#1118 Beecrowd Online Judge Solution 1118 Several Scores with Validation Solution in C++, Java, Js and Python