936A. Save Energy! - Codeforces Solution C++

  Problem Link : 936A. Save Energy! 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

#define dl long double


int main()
{
	ll k,d,t;
	dl ans=0;
	cin>>k>>d>>t;
	if(k%d==0)
		cout<<t;
	else
	{
		ll m;
		if(d>k)
			m=d-k;
		else
		{
		    m=k/d;
		    m++;
		//	m=ceil(dl(k)/d);
			m*=d;
			m-=k;
		}
		dl frac=(dl)k/t;
		frac+=(dl)m/(2*t);
//		cout<<frac<<endl;
		ll n=floor(1/frac);
	    dl rem=1-n*(frac);
		ans+=n*(k+m);
//		cout<<n<<" "<<ans<<endl;
		if(rem==0)
		{
			cout<<ans;
			exit(0);
		}		
		dl x=t*rem;
      //  cout<<x<<endl;
		if(x<=k)
			ans+=x;
		else
		{
		  //  cout<<rem<<endl;
			rem-=(dl)k/t;
	//		 cout<<rem<<endl;
			ans+=k;
		//	cout<<ans<<endl;
		//	cout<<rem<<endl;
			ans+=rem*2*t;
		}
		cout<<fixed<<setprecision(10)<<ans;
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!