# BOLT - Editorial

Author & Editorialist: Daanish Mahajan
Testers: Shubham Jain, Aryan Choudhary

Cakewalk

Handling floats

# PROBLEM:

Given Chef’s maximum speed (v m/sec) during his practice and the different factors (K1, K2, K3) affecting his speed in competition with respect to practice, tell whether his final speed in the competition after considering all the factors will be sufficient to run a distance of 100 m in less than 9.58 seconds.

# EXPLANATION:

Chef’s best speed in competition after considering all the factors (v') = Maximum Speed during practice (v) * K1 * K2 * K3

TIme taken to complete a distance of 100 m in competition (T) = \frac{100}{v'} which should be strictly less than 9.58 seconds.

Note: T is to be rounded to 2 decimal places before comparison.

# IMPLEMENTATION:

To avoid using floating-point values in the calculation, initially take the input (K1, K2, K3, v) in form of strings and convert each variable to an integer removing the decimal point.
So v' = v * K1 * K2 * K3 * 10^{-5} since each of K1, K2, K3 contributes 1 digit after the decimal and v contributes 2 digits after decimal.

T = \frac{100}{v * K1 * K2 * K3 * 10^{-5}} < 9.575 since numbers in the range [9.575, 9.58] will be rounded to 9.58.

Rearranging we get:
v * K1 * K2 * K3 * 9575 > 10^{10}

The same can be solved by keeping the input in floats and using doubles during calculation.

# SOLUTIONS:

Setter's Solution
                #include<bits/stdc++.h>

using namespace std;

const int maxt = 1e5;
const string newln = "\n", space = " ";

double round2(double var)
{
char str[40];
sprintf(str, "%.2lf", var);
sscanf(str, "%lf", &var);
return var;
}

int main()
{
int t; cin >> t;
double k1, k2, k3, v;
while(t--){
cin >> k1 >> k2 >> k3 >> v;
assert(0 < k1 && k1 < 2); assert(0 < k2 && k2 < 2); assert(0 < k3 && k3 <= 1); assert(9 < v && v < 11);
double time = round2(100.0 / (v * k1 * k2 * k3));
string ans = time < 9.58 ? "YES" : "NO";
cout << ans << endl;
}
}

Tester's Solution
                #include<bits/stdc++.h>

using namespace std;

const long long int mod = 1000000007L;

long long int T,n,i,j,k,in,cnt,l,r,u,v,x,y;
string ks1, ks2, ks3, vs;
long long int m;
string s;

long long int ToFloat(string s,long long int f)
{
const long long int n = s.size();
assert(s[n-f-1]=='.');
long long int v=0;
for(auto x:s)
{
if(x=='.')
continue;
v=v*10+x-'0';
}
return v;
}

int main(void) {
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> T;
while(T--)
{
cin >> ks1 >> ks2 >> ks3 >> vs;
const long long int den=ToFloat(vs,2)*ToFloat(ks1,1)*ToFloat(ks2,1)*ToFloat(ks3,1);
const long long int num=1e7;
cout<<(num*1000<9575*den?"YeS":"nO")<<endl;
}
return 0;
}


The contest is still running! How can the editorial be released right now?!

2 Likes

#include
using namespace std;
int main() {
int t;
cin>>t;
string out[t];
for(int i = 0; i < t; i++){
float k1, k2, k3, v;
cin>>k1>>k2>>k3>>v;
float m = 100/(k1 * k2 * k3 * v);
cout<<m<<endl;
m = (int)(m * 100 + 0.5);
cout<<m<<endl;
m = (float)m/100;
printf("%.5f\n", m);
if(m >= 9.58){
out[i] = “NO”;
}
else{
out[i] = “YES”;
cout<<m<<endl;
}
}
for(int i = 0; i < t; i++){
cout<<out[i]<<endl;
}
return 0;
}

Can someone why my code is showing YES for 9.58

1 Like

#include
using namespace std;
int main(){
int t;
cin>>t;
while(t-- >0) {
float k1, k2, k3, v;
cin >> k1 >> k2 >> k3 >> v;

    float s;
s= k1*k2*k3*v;
s=100/s;
s=(int)(s*100+0.5);
s=(float)(s/100);

if(s<9.58){
cout<<"YES";
}
else
cout<<"NO";
}


}

can someone please tell why it shows yes for 9.58

try comparing m>=9.58f

I did it now thanks

but how?