zoukankan      html  css  js  c++  java
  • CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.

    As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each d meters of the path. We know that Valera starts at the point with coordinates (0, 0) and runs counter-clockwise. That is, when Valera covers a meters, he reaches the point with coordinates (a, 0). We also know that the length of the marathon race equalsnd + 0.5 meters.

    Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers d, 2·d, ..., n·d meters.

    Input

    The first line contains two space-separated real numbers a and d (1 ≤ a, d ≤ 105), given with precision till 4 decimal digits after the decimal point. Number a denotes the length of the square's side that describes the stadium. Number d shows that after each d meters Valera gets an extra drink.

    The second line contains integer n (1 ≤ n ≤ 105) showing that Valera needs an extra drink n times.

    Output

    Print n lines, each line should contain two real numbers xi and yi, separated by a space. Numbers xi and yi in the i-th line mean that Valera is at point with coordinates (xi, yi) after he covers i·d meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10 - 4.

    Note, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem.

    Sample test(s)
    input
    2 5
    2
    
    output
    1.0000000000 2.0000000000
    2.0000000000 0.0000000000
    
    input
    4.147 2.8819
    6
    
    output
    2.8819000000 0.0000000000
    4.1470000000 1.6168000000
    3.7953000000 4.1470000000
    0.9134000000 4.1470000000
    0.0000000000 2.1785000000
    0.7034000000 0.0000000000

    分析如图所看到的:


    给出正方形跑道的边长a。每走d米求一次坐标。

    浮点数的取模处理。当时做题的时候不知道浮点数取模函数:fmod()

    百度百科:fmod

    fmod - C函数名: fmod

    功 能:计算x对y的模,即x/y的求余运算 。若y是0。则返回NaN
    用 法:double fmod(double x,double y);
    须要头文件: math.h

    例程:

    #include < stdio.h>
      #include < math.h>
      int main(void)
       {
          double x = 5.0,y = 2.0;
          double result;
          result = f mod(x,y);
          printf("The remainder of (%lf / %lf) is \%lf
    ",x,y,result);
          return 0; 
        }

    执行结果是:The remainder of (5.000000/2.000000) is 1.000000

    知道这个函数之后。题目就很好做了:

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int Inf=(1<<31)-1;
    const double Eps=1e-15;
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        double a,d,l,s;
        int i,j,n;
        while(scanf("%lf%lf",&a,&d) !=EOF){
            scanf("%d",&n);
            l=a*4.0;
            for(i=1; i<=n; i++){
                s=fmod(i*d,l);
                int flag=1;
                while(s-a >0){
                    s-=a;
                    flag++;
                }
                if(fabs(s-a)<Eps){
                    s=0.0;
                    flag++;
                }
                
                if(flag ==1){
                    printf("%.10lf %.10lf
    ",s,0.0);
                }
                else if(flag == 2){
                    printf("%.10lf %.10lf
    ",a,s);
                }
                else if(flag == 3){
                    printf("%.10lf %.10lf
    ",a-s,a);
                }
                else {
                    printf("%.10lf %.10lf
    ",0.0,a-s);
                }
    
            }
    
        }
        return 0;
    }
    

    精简代码:

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int Inf=(1<<31)-1;
    const double Eps=1e-1;
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        double a,d,x,y,s=0.0;
        int n;
        scanf("%lf%lf%d",&a,&d,&n);
        for(int i=1; i<=n; i++){
            s+=d;
            s=fmod(s,4*a);
            if(s <= a)x=s,y=0;
            else if(s <=2*a)x=a,y=s-a;
            else if(s <=3*a)x=a-(s-2*a),y=a;
            else x=0,y=a-(s-3*a);
            printf("%lf %lf
    ",x,y);
        }
        return 0;
    }
    





  • 相关阅读:
    053-146
    053-659
    053-658
    053-657
    053-656
    053-654
    053-655
    053-652
    053-651
    053-650
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5246514.html
Copyright © 2011-2022 走看看