+ 4
Halloween candy
#include <iostream> using namespace std; int main() { int houses; cin>>houses; //your code goes here float probab=2.0/houses; int res = probab*100; if(probab<=0.5) cout<<res<<endl; else cout<<++res<<endl; return 0; } Can anyone tell me ...what's wrong with this and why I passed 2 cases but failed to pass the other 3!!
13 Respuestas
+ 5
Vaibhav Tyagi Condition is houses should be greater that or equal to 3. So
if(houses >= 3) {
res = 2.0 * 100 / houses;
}
res = ceil(res);
cout << (int) res;
+ 7
AJ #Infinity Love voila ...it worked !! Thankyou
+ 3
Yeah I am getting...can you tell me what are the other three cases ! ...is this logic wrong to get the nearest rounded value??
+ 3
AJ #Infinity Love ohk ...I will try this !
+ 3
Rithea Sreng the logic of if(probab<=0.5) is because
Probab is always 0-1 in this program !
So if I explicitly type convert it then it'll always drop the decimal part and return the lower value of int ...which is acceptable when the decimal part is less then 0.5 but when the decimal part is greater than 0.5 it is to incremented by one.
But if you are saying that ...it is always have to be rounded up ...then you are right! 😅
+ 2
Vaibhav Tyagi We can't see other cases.
+ 2
John Egan I didn't get you !! Is there a problem ?
+ 1
Vaibhav Tyagi You should get nearest whole number means rounded value.
res = ceil(res);
cout << (int) res;
0
Wt voogle u guy,s gal,s texting about hummm i gotta learn yup...
0
#include <iostream>
using namespace std;
int main() {
int houses;
cin>>houses;
float res;
if(houses >= 3) {
res = 2.0 * 100 / houses;
}
res = res+ 0.99;
cout << (int) res;}