Algorithm


Problem Name: beecrowd | 1172

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

Array Replacement I

 

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

Read an array X[10]. After, replace every null or negative number of X ​by 1. Print all numbers stored in the array X.

 

Input

 

The input contains 10 integer numbers. These numbers ​​can be positive or negative.

 

Output

 

For each position of the array, print "X [i] = x", where i is the position of the array and x is the number stored in that position.

 

 

 

Input Sample Output Sample

0
-5
63
0
...

X[0] = 1
X[1] = 1
X[2] = 63
X[3] = 1
...

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    int lista[10];
    for(int i = 0; i  <  10; i++){
        int n;
        cin >> n;
        if(n == 0 or n < 0){
            lista[i] = 1;
        }
        else{
            lista[i] = n;
        }
    }
    for(int i = 0; i  <  10; i++>{
        cout << "X[" << i << "] = " << lista[i] << "\n";
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
0 -5 63 0 ...

Output

x
+
cmd
X[0] = 1 X[1] = 1 X[2] = 63 X[3] = 1 ...

#2 Code Example with Javascript Programming

Code - Javascript Programming


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

let lista = [];
for(var i = 0; i  <  10; i++){
    let n = Number(lines.shift());
    n === 0 || n  <  0 ? lista.push(1) : lista.push(n);
}
for(var l = 0; l  <  10; l++){
    console.log(`X[${l}] = ${lista[l]}`);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
0 -5 63 0 ...

Output

x
+
cmd
X[0] = 1 X[1] = 1 X[2] = 63 X[3] = 1 ...

#3 Code Example with Python Programming

Code - Python Programming


lista = []
for i in range(0, 10):
    n = int(input())
    if n == 0 or n < 0:
        lista += [1]
    else:
        lista += [n]
for i in range(0, 10):
    print(f'X[{i}] = {lista[i]}')
Copy The Code & Try With Live Editor

Input

x
+
cmd
0 -5 63 0 ...

Output

x
+
cmd
X[0] = 1 X[1] = 1 X[2] = 63 X[3] = 1 ...
Advertisements

Demonstration


Previous
#1171 Beecrowd Online Judge Solution 1171 Number Frequence Solution in C, C++, Java, Js and Python
Next
#1173 Beecrowd Online Judge Solution 1173 Array fill I Solution in C++, Java, Js and Python