Created
November 25, 2015 00:39
-
-
Save anonymous/1d790374c8525c280273 to your computer and use it in GitHub Desktop.
solution for problem on hackerrank (https://www.hackerrank.com/challenges/plus-minus), It passes any of the testcases I give it including ones that the site says it fails when checking for solutions. I'm not sure what the problem is, I'm assuming it's a float precision problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*solution for problem on hackerrank (https://www.hackerrank.com/challenges/plus-minus), It passes any of the | |
testcases I give it including ones that the site says it fails when checking for solutions. | |
I'm not sure what the problem is, but I'm assuming it's a float precision problem | |
is supposed to take scan in an array of numbers of length given by the first number and then print the ratio of positive/ negative | |
and zero numbers to the third decimal place*/ | |
#include <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <limits.h> | |
#include <stdbool.h> | |
int main(){ | |
int n, pos, neg, arr_i; | |
scanf("%d",&n); | |
int arr[n]; | |
pos = 0; | |
neg = 0; | |
for(arr_i = 0; arr_i < n; arr_i++){ | |
scanf("%d",&arr[arr_i]); | |
if (arr[arr_i] != 0){ | |
if (arr[arr_i] > 0){ | |
pos +=1; | |
} | |
else{ | |
neg +=1; | |
} | |
} | |
} | |
printf("%.3f\n", ((pos*1.0)/n)); | |
printf("%.3f\n", ((neg*1.0)/n)); | |
printf("%.3f", ((1.0*(6-(neg+pos)))/n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment