编程介的小学生
2018-12-15 13:31
采纳率: 92.7%
浏览 2.6k

C语言要求计算结果保留2位小数?距离的计算怎么实现?

Problem Description
There is a curious man called Matt.

One day, Matt's best friend Ted is wandering on the non-negative half of the number line. Matt finds it interesting to know the maximal speed Ted may reach. In order to do so, Matt takes records of Ted’s position. Now Matt has a great deal of records. Please help him to find out the maximal speed Ted may reach, assuming Ted moves with a constant speed between two consecutive records.

Input
The first line contains only one integer T, which indicates the number of test cases.

For each test case, the first line contains an integer N (2 ≤ N ≤ 10000),indicating the number of records.

Each of the following N lines contains two integers ti and xi (0 ≤ ti, xi ≤ 106), indicating the time when this record is taken and Ted’s corresponding position. Note that records may be unsorted by time. It’s guaranteed that all ti would be distinct.

Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1), and y is the maximal speed Ted may reach. The result should be rounded to two decimal places.

Sample Input
2
3
2 2
1 1
3 4
3
0 3
1 5
2 0

Sample Output
Case #1: 2.00
Case #2: 5.00

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • blownewbee 2019-08-12 23:50
    已采纳
    点赞 评论
  • SD_LTF 2018-12-16 01:12
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 10010;
    struct node
    {
        int t, x;
    }a[maxn];
    
    bool cmp(node a, node b)
    {
        return a.t < b.t;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        for(int k = 1; k <= t; k++)
        {
            int n;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
                scanf("%d%d", &a[i].t, &a[i].x);
            sort(a, a + n, cmp);
            double ans = 0;
            for(int i = 1; i < n; i++)
            {
                double cur = (a[i].x - a[i - 1].x) * 1.0 / (a[i].t - a[i - 1].t);
                cur = fabs(cur);
                ans = max(ans, cur);
            }
            printf("Case #%d: %.2f\n", k, ans);
        }
        return 0;
    }
    
    

    转自这个大佬

    点赞 评论

相关推荐 更多相似问题