hdu 1275纯数学题

找出足够的相遇或追及的时间,排个序输出就可以了。

/*
 * hdu1275/win.cpp
 * Created on: 2012-10-23
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef struct Node{
    double t;
    double dis;
    Node() {
        dis = t = 0;
    }
    Node(double tt, double dd) {
        t = tt;
        dis = dd;
    }
}Node;
inline bool operator<(const Node &n1, const Node &n2) {
    return n1.t < n2.t;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int T, n;
    double S, v1, v2, v3, v4;
    scanf("%d", &T);
    while(T--) {
        set<Node> myset;
        scanf("%lf%lf%lf%d", &S, &v1, &v2, &n);
        v3 = v1 + v2;
        v4 = fabs(v1 - v2);
        for(int k = 1; k < 3000; k += 2) {
            double t = k * S / v3;
            double dis = t * v1;
            dis = dis - (int)(dis / S) * S;
            if(2 * dis > S) {
                dis = S - dis;
            }
            myset.insert(Node(t, dis));
            t = k * S / v4;
            dis = t * v1;
            dis = dis - (int)(dis / S) * S;
            if(2 * dis > S) {
                dis = S - dis;
            }
            myset.insert(Node(t, dis));
        }
        set<Node>::iterator it = myset.begin();
        while(n-- > 1) {
            it++;
        }
        printf("Time=%.3f Dist=%.3f\n", (*it).t, (*it).dis);
    }
    return 0;
}
posted @ 2012-10-23 19:55  moonbay  阅读(160)  评论(0编辑  收藏  举报