Given an array of integers, calculate which fraction
of the elements are positive, negative, and zeroes, respectively. Print
the decimal value of each fraction.
Input Format
The first line, N , is the size of the array.
The second line containsN space-separated integers describing the array of numbers (A1,A2,A3,⋯,AN ).
The second line contains
Output Format
Print each value on its own line with the fraction of positive numbers first, negative numbers second, and zeroes third.
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Explanation
There are 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The fraction of the positive numbers, negative numbers and zeroes are36=0.500000 , 26=0.333333 and 16=0.166667 , respectively.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to10−4 are acceptable.
-----------------------------------------------------------------------------------------------------------
The fraction of the positive numbers, negative numbers and zeroes are
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to
-----------------------------------------------------------------------------------------------------------
Plus Minus - Hacker Rank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N, j=0, z=0, h=0, l=0;
cin >> N;
int nr[N];
for (int i=0; i<N; i++){
cin >> nr[i];
}
while (j<N){
if (nr[j]>0){
h++;
}
if (nr[j]<0){
l++;
}
else if (nr[j]==0) {
z++;
}
j++;
}
cout << (float)h/N << endl << (float)l/N << endl << (float)z/N;
return 0;
}
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N, j=0, z=0, h=0, l=0;
cin >> N;
int nr[N];
for (int i=0; i<N; i++){
cin >> nr[i];
}
while (j<N){
if (nr[j]>0){
h++;
}
if (nr[j]<0){
l++;
}
else if (nr[j]==0) {
z++;
}
j++;
}
cout << (float)h/N << endl << (float)l/N << endl << (float)z/N;
return 0;
}