Algorithm


Problem Name: beecrowd | 1118

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

beecrowd | 1118

Several Scores with Validation

By Neilor Tonin, URI Brazil

Timelimit: 1

Write an program to read two scores of a student. Calculate and print the semester average. The program must accept only valid scores (a score must fit in the range [0.10]). Each score must be validated separately.

The program must print a message "novo calculo (1-sim 2-nao)" that means "new calculate (1-yes 2-no)". After, the input will be (1 or 2). 1 means a new calculation, 2 means that the execution must be finished.

Input

The input file contains several positive or negative floating-point (double) values​. After the input of 2 valid scores, an integer number X will be read. Your program must stop when X = 2.

Output

If an invalid score is read, must be printed the message "nota invalida". When two valid scores are read, the message "media = " must be printed folowed by the average between these 2 scores. The message "novo calculo (1-sim 2-nao)" must be printed after reading X. This message should be displayed again if the standard input number for X is less than 1 or greater than 2, as example below.

The output average must be printed with 2 digits after the decimal point.

Input Sample Output Sample

-3.5
3.5
11.0
10.0
4
1
8.0
9.0
2

nota invalida
nota invalida
media = 6.75
novo calculo (1-sim 2-nao)
novo calculo (1-sim 2-nao)
media = 8.50
novo calculo (1-sim 2-nao)

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 newNum;
    while(1){
        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;
        while(1){
            cout << "novo calculo (1-sim 2-nao)" << endl;
            cin >> newNum;
            if (newNum  <  1 || newNum > 2){
                continue;
            }
            else{
                break;
            }
        }
        if (newNum == 2){
            break;
        }
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0 4 1 8.0 9.0 2

Output

x
+
cmd
nota invalida nota invalida media = 6.75 novo calculo (1-sim 2-nao) novo calculo (1-sim 2-nao) media = 8.50 novo calculo (1-sim 2-nao)

#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 newNum;
        while(true){
            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));

            while(true){
                System.out.println("novo calculo (1-sim 2-nao)");
                newNum = input.nextInt();
                if (newNum  <  1 || newNum > 2){
                    continue;
                }
                else{
                    break;
                }
            }
            if (newNum == 2){
                break;
            }
        }
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0 4 1 8.0 9.0 2

Output

x
+
cmd
nota invalida nota invalida media = 6.75 novo calculo (1-sim 2-nao) novo calculo (1-sim 2-nao) media = 8.50 novo calculo (1-sim 2-nao)

#3 Code Example with Javascript Programming

Code - Javascript Programming


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

var newNum;
while(true){
    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));
    while(true){
        console.log("novo calculo (1-sim 2-nao)");
        newNum = parseInt(lines.shift());
        if (newNum  <  1 || newNum > 2){
            continue;
        }
        else{
            break;
        }
    }
    if (newNum == 2){
        break;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0 4 1 8.0 9.0 2

Output

x
+
cmd
nota invalida nota invalida media = 6.75 novo calculo (1-sim 2-nao) novo calculo (1-sim 2-nao) media = 8.50 novo calculo (1-sim 2-nao)

#4 Code Example with Python Programming

Code - Python Programming


while(True):
    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))
    newNum = 0
    while(True):
        print("novo calculo (1-sim 2-nao)")
        newNum = int(input())
        if (newNum == 1 or newNum == 2):
            break

    if (newNum == 2):
        break
Copy The Code & Try With Live Editor

Input

x
+
cmd
-3.5 3.5 11.0 10.0 4 1 8.0 9.0 2

Output

x
+
cmd
nota invalida nota invalida media = 6.75 novo calculo (1-sim 2-nao) novo calculo (1-sim 2-nao) media = 8.50 novo calculo (1-sim 2-nao)
Advertisements

Demonstration


Previous
#1117 Beecrowd Online Judge Solution 1117 Score Validation Solution in C++, Java, Js and Python
Next
#1121 Beecrowd Online Judge Solution 1121 Sticker Collector Robot Solution in C, C++, Java, Js and Python