Codeforces Round #237 Div.2 B
题目链接:http://codeforces.com/contest/404/problem/B
模拟题,但是数据很大,可能会溢出。所以要使用一些防止溢出。
刚开始写没有注意防止溢出,结果过不了后面的数据,下面是没有通过的代码:
///2014.3.19 ///Codeforces Round #237 Div.2 ///B #include <iostream> #include <cstdio> using namespace std; int main() { // freopen("in","r",stdin); // freopen("out","w",stdout); double a,d; cin>>a>>d; double n; cin>>n; double zero = 0.0; for(int i=1 ; i<=n ; i++){ double l = i*d-int(i*d/(4*a))*4*a; if( l<=a ){ printf("%.10lf %.10lf\n",l,zero ); } else if( l<=2*a ){ printf("%.10lf %.10lf\n",a,l-a ); } else if( l<=3*a ){ printf("%.10lf %.10lf\n",3*a-l,a ); } else{ printf("%.10lf %.10lf\n",zero,4*a-l ); } } return 0; }
WA后看了别人的代码,模仿了一个防止溢出的方法。下面是模仿别人的代码,通过了。
1 ///2014.3.19 2 ///Codeforces Round #237 Div.2 3 ///B 4 5 #include <iostream> 6 #include <cstdio> 7 using namespace std; 8 9 int main() 10 { 11 // freopen("in","r",stdin); 12 // freopen("out","w",stdout); 13 14 double a,d; 15 cin>>a>>d; 16 int n; 17 cin>>n; 18 double zero = 0.0; 19 double x,y; 20 x = d; 21 y = 0; 22 int nn = 0; 23 while( x>=a ){ 24 x -= a; 25 nn++; 26 } 27 int t = 0; 28 for(int i=1 ; i<=n ; i++){ 29 y += x; 30 t += nn; 31 while( y>=a ){ 32 y -= a; 33 t++; 34 } 35 switch( t%4 ){ 36 case 0: 37 printf("%.10lf %.10lf\n",y,zero ); break; 38 case 1: 39 printf("%.10lf %.10lf\n",a,y); break; 40 case 2: 41 printf("%.10lf %.10lf\n",a-y,a ); break; 42 case 3: 43 printf("%.10lf %.10lf\n",zero,a-y ); break; 44 } 45 } 46 47 return 0; 48 }
codeforces比赛结束了可以看别人的代码,还有题解,挺不错的。就是比赛时间太坑了,上海时间11:30到凌晨1:30 (>﹏<)。等到下学期回到石河子就好啦,^_^