zoukankan      html  css  js  c++  java
  • 浮点数取模

    题目链接:http://codeforces.com/contest/404/problem/B

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    double a, d;
    int n;
    
    int main()
    {
        cin >> a >> d >> n;
        int cnt = 0;
        double len = 0;
        for (int i = 1; i <= n; i++)
        {
            len += d;
            int temp = len / a;
            len = len - temp * a;
            cnt += temp;
            cnt %= 4;
            if (cnt == 0)
            {
                printf("%.10lf %.10lf
    ", len, 0.0);
            }
            else if (cnt == 1)
            {
                printf("%.10lf %.10lf
    ", a, len);
            }
            else if (cnt == 2)
            {
                printf("%.10lf %.10lf
    ", a - len, a);
            }
            else
            {
                printf("%.10lf %.10lf
    ", 0.0, a - len);
            }
        }
        return 0;
    }

     也可以用浮点数取模函数fmod(a,b);

    #include <cstdio>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        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;
    }
  • 相关阅读:
    Socket listen 简要分析
    Socket connect 等简要分析
    HIVE函数大全
    元数据管理
    flume
    shell编程
    数据仓库集锦
    数据库知识
    hive sql 转化mapreduce原理
    Hadoop 学习笔记
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5362491.html
Copyright © 2011-2022 走看看