UVA 4561 - Range

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2562
题        意:给你多组a,b此地行驶到起点的距离,b为此时的燃料量,求最后剩下的燃料可以走多远
思        路:将每一个递减的且相邻的两组数之间的距离以及燃料减少的量各自相加,求出每一份燃料

                    可以走的距离,再乘以最后剩下的燃料

代码如下:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
typedef long long LL;

double a[10000],b[10000];
int main()
{
    //cout<<19%1<<endl;
    while( scanf ( "%lf %lf", &a[0], &b[0] ) != EOF )
    {
        if(a[0] == -1 && b[0] == -1) break;
        int i = 1;
        while(1)
        {
            cin >> a[i] >> b[i];
            if(a[i] == 0 && b[i] == 0) break;
            i ++;
        }
        double now1 = 0, now2 = 0;
        for( int j = 1; j < i; j++ )
        {
            if( b[j-1] > b[j] ) //判断是否递减,因为若大于,则添加了燃料无法判断这段距离的消耗
            {
                now1 += ( a[j] - a[j-1] );
                now2 += ( b[j-1] - b[j] );
            }
        }
        double ans = ( b[i-1]*now1/now2 ) * 100;//注意精确度,有点坑
        int an = ( ans + 50.0 ) / 100;
        printf( "%d\n", an );
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值