A. Range
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format:
%I64d Java class name:
Main
Some automobiles display the estimated driving range, that is, the distance you can expect to drive it (without adding fuel) before running out of fuel. Here is how it works: periodically, the vehicle's computer records the odometer reading and the weight of fuel in the fuel tank. From this data, the fuel consumption over a certain distance can be computed.
From the fuel consumption and the most recent measurement of fuel tank contents (which we assume is current for all practical purposes), the range can be calculated. Intervals over which the quantity of fuel increased (fuel was added to the tank) will not be used in the computations. For example, in the first problem instance of the sample input, the interval where the fuel weight increased from 29.9 kilograms to 34.2 kilograms will not be used. In this example, 16.3 kilograms of fuel were consumed over a distance of 228.6
kilometers. Therefore, the most recently measured fuel contents of 31.2 kilograms will enable you to drive another 438 kilometers (rounded to the nearest integer). The input will always contain at least one interval (two consecutive lines of input) where no fuel was added to the tank.
From the fuel consumption and the most recent measurement of fuel tank contents (which we assume is current for all practical purposes), the range can be calculated. Intervals over which the quantity of fuel increased (fuel was added to the tank) will not be used in the computations. For example, in the first problem instance of the sample input, the interval where the fuel weight increased from 29.9 kilograms to 34.2 kilograms will not be used. In this example, 16.3 kilograms of fuel were consumed over a distance of 228.6
kilometers. Therefore, the most recently measured fuel contents of 31.2 kilograms will enable you to drive another 438 kilometers (rounded to the nearest integer). The input will always contain at least one interval (two consecutive lines of input) where no fuel was added to the tank.
Input
The input contains data for a number of problem instances. Each problem instance consists of three or more (odometer reading, fuel weight) pairs, one pair per line. Distances are measured in kilometers and fuel weight in kilograms. All input numbers will be given to one decimal place. The end of each problem instance will be signaled by a (0.0, 0.0) pair. The last problem instance will be followed by a (-1.0, -1.0) pair.
Output
For each problem instance, print the range, rounded to the nearest integer.
Sample Input
18400.5 43.2 18440.4 40.4 18482.7 37.0 18540.2 33.1 18585.3 29.9 18620.8 34.2 18664.6 31.2 0.0 0.0 18400.5 43.2 18440.4 40.4 18482.7 37.0 18540.2 33.1 18585.3 29.9 0.0 0.0 -1.0 -1.0
Sample Output
438
415
啊啊啊啊啊,我发现自己好讨厌啊!
按照思路没错啊,怎么就是不行呢?呜呜呜
原来不按照题目写题的孩子不是好孩子,
特此提醒广大小胖妞:
A==0&&B==0 的反面是 A!=0||B!=0
不要乱写
#include <iostream>
#include<stdio.h>
#include <algorithm>
using namespace std;
const double T=1e9+9;
struct node
{
double a,b;
}N[12345];
int main()
{
double A,B;
int m = 0;
while(cin>>A>>B)
{
if(A == -1.0 &&B == -1.0)
break;
else if(A!=0 || B !=0)//呜呜呜,数学学了那么久,
{ N[m].a = A,N[m++].b = B; // 怎么还搞不清楚这个问题,A==0&&B==0的反面是什么?
}
else
{
N[m].a=T;
N[m].b=T;
double key_a = N[0].a;
double key_b = N[0].b;
double sum = 0.0,tt = 0.0,ans;
for(int i=0;i<m;i++)
{
if( N[i].b < N[i+1].b)
{ sum += N[i].a - key_a;
tt += key_b - N[i].b;
key_a = N[i+1].a;
key_b = N[i+1].b;
}
}
ans =(sum/tt)*N[m-1].b;
printf("%.0lf\n",ans);
m = 0;
}
}
return 0;
}
所以你最好不要这么写 ,按照正常的想法来
#include <iostream>
#include<stdio.h>
#include <algorithm>
using namespace std;
const double T=1e9+9;
struct node
{
double a,b;
}N[12345];
int main()
{
double A,B;
int m = 0;
while(cin>>A>>B)
{
if(A == -1.0 &&B == -1.0)
break;
else if(A==0 && B ==0)
{
N[m].a=T;
N[m].b=T;
double key_a = N[0].a;
double key_b = N[0].b;
double sum = 0.0,tt = 0.0,ans;
for(int i=0;i<m;i++)
{
if( N[i].b < N[i+1].b)
{ sum += N[i].a - key_a;
tt += key_b - N[i].b;
key_a = N[i+1].a;
key_b = N[i+1].b;
}
}
ans =(sum/tt)*N[m-1].b;
printf("%.0lf\n",ans);
m = 0;
}
else
{
N[m].a = A,N[m++].b = B;
}
}
return 0;
}