1. 程式人生 > >C. Save Energy!

C. Save Energy!

Note

In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5

minutes the chicken will be ready .

In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes.

題的思路比較友好,不難。但是,我的基礎比較爛,一些個函式的用法不明確,導致出錯。

abs( )主要用於對求整數的絕對值,在“stdlib.h”(或<cstdlib>)標頭檔案裡面。

fabs( )主要是求精度要求更高的double ,float型的絕對值,在<cmath>標頭檔案裡。

兩者在只#include<cmath>時都可以使用。

所以我一直abs,一直wa.



#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<cstring>
typedef long long ll;
const double eps=1e-9;
int main()
{
	ll k,d,t;
	scanf("%lld %lld %lld",&k,&d,&t);
	if(k>=d&&k%d==0) {
		printf("%lld\n",t);
	} else {
		ll tmp=(ll)(k/d);
		ll min=(tmp+1)*d;//min time
		double val=(double)k+(min-k)*0.5;
		ll ans=(ll)t*1.0/val;
		double ed=(double)t,kk=(double)k,aans=(double)ans,mmin=(double)min;
		if(fabs(val*aans-ed)<=eps){
			printf("%.9f\n",aans*min);
		}
		else{
			double now=(aans)*val;
			if(ed-now>kk)
				printf("%.9f\n",aans*mmin+kk+(ed-now-kk)*2.0);
			else
				printf("%.9f\n",aans*mmin+(ed-now));
		}
	}
	return 0;
}