CodeForces

题意:一个人绕着一个长度为a的正方形逆时针跑,以(0,0)为起点,喝一次水可以跑d米,问每喝一次水可以跑到的位置坐标。

分析:这道题卡精度卡的太厉害了。

设l是正方形的周长,只有d对l取余且每次跑d米都对l取余,并用取余后的结果继续跑,这样的精度才够。

1、double a = fmod(x, y);返回的是浮点数x对浮点数y取余后的结果。

2、每跑d米,通过对l取余得到当前位置与起点的路径长,从而确定当前位置的坐标。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-12;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int main(){
    double a, d;
    int n;
    scanf("%lf%lf%d", &a, &d, &n);
    double sum = 0;
    double l = 4 * a;
    d = fmod(d, 4 * a);
    while(n--){
        sum += d;
        sum = fmod(sum, l);
        double tmp = fmod(sum, a);
        if(dcmp(sum, a) < 0){
            printf("%.10f 0.0000000000
", tmp);
        }
        else if(dcmp(sum, 2 * a) < 0){
            tmp = fmod(tmp, a);
            printf("%.10f %.10f
", a, tmp);
        }
        else if(dcmp(sum, 3 * a) < 0){
            tmp = fmod(tmp, a);
            printf("%.10f %.10f
", a - tmp, a);
        }
        else{
            tmp = fmod(tmp, a);
            printf("0.0000000000 %.10f
", a - tmp);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7113569.html