【C++】向上、向下取整函数
C/C++ 取整函数ceil(),floor(),向上取整,向下取整
C-Car
Ruins is driving a car to participating in a programming contest. As on a very tight schedule, he will drive the car without any slow down, so the speed of the car is non-decrease real number.
Of course, his speeding caught the attention of the traffic police. Police record N positions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 0
.
Now they want to know the minimum time that Ruins used to pass the last position.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case begins with an integers N, which is the number of the recorded positions.
The second line contains N numbers a1, a2, ⋯, aN, indicating the recorded positions.
Limits
1≤T≤100
1≤N≤105
0<ai≤109
ai<ai+1
Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the minimum time.
Sample Input
1
3
6 11 21
Sample Output
Case #1: 4
题意:
一辆汽车从0点开始出发,给你几个他的时间点到达的地点,但这几个时间点不一定是连续的,问你到达给出的最后一个时间点所花费的时间为多少,对于速度有个要求:后一秒走的路程比大于等于前一秒
1.经过题中给出的地点时,时间点要为整数,车速要非递减
2.就是让车一直加速,但加速度可变,并且在经过题中给出的两点之间距离时用了整数的时间
思路:
在到达最后一个地点时,速度达到最大
经过最后一段路程时,时间为1
所以从后往前推时间和速度
前一段距离的速度 <= 后一段距离的速度
赛后补题:AC
参考了下题解
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
long long int x[100005];
scanf("%d",&n);
x[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&x[i]);
}
long long int sum=0,ti;
for(int i=n;i>=1;i--)
{
if(i==n) ti=1;
else
{
ti=(int)ceil((x[i]-x[i-1])*1.0*ti/(x[i+1]-x[i]));//向上取整
}
sum+=ti;
// printf("***%d %f %d %d\n",x[i]-x[i-1],v,t,sum);
}
printf("Case #%d: %lld\n",k,sum);
}
return 0;
}
比赛时写的代码:WA
冗杂。。。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
for(int k=1; k<=t; k++)
{
int st[100005]= {0};
double v[100005]= {0};
long long int tim[100005]= {0};
long long int sum=0;
scanf("%d",&n);
int m=0;
int x,y;
scanf("%d",&x);
if(n==0)
sum=1;
else
{
st[m++]=x;
while(m<n)
{
scanf("%d",&y);
st[m++]=y-x;
x=y;
}
for(int i=m-1; i>=0; i--)
{
if(i==m-1)
{
tim[i]=1;
v[i]=st[i];
}
// else if(st[i]<st[i+1])
// {
// tim[i]=st[i]*1.0/v[i+1];
// if(tim[i]*v[i+1]!=st[i]) tim[i]++;
// v[i]=st[i]*1.0/tim[i];
// }
// else if(st[i]>=st[i+1])
// {
else
{
tim[i]=st[i]*1.0/v[i+1];
if(tim[i]*v[i+1]!=st[i]) tim[i]++;
v[i]=st[i]*1.0/tim[i];
}
// }
sum+=tim[i];
// printf("%d %.2f %lld\n",st[i],v[i],tim[i]);
}
}
printf("Case #%d: %lld\n",k,sum);
}
}
修改之后的代码:AC
精简版见文章开头的第一个代码
它这两个代码一个WA一个AC
主要问题就是:
1.long long int 的问题
2.每一段路程其速度的精度问题
解决:
2.不求速度,速度随用随算
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
for(int k=1; k<=t; k++)
{
int st[100005]= {0};
long long int tim[100005]= {0};
long long int sum=0;
scanf("%d",&n);
int m=0;
int x,y;
scanf("%d",&x);
if(n==0)
sum=1;
else
{
st[m++]=x;
while(m<n)
{
scanf("%d",&y);
st[m++]=y-x;
x=y;
}
for(int i=m-1; i>=0; i--)
{
if(i==m-1)
{
tim[i]=1;
}
else
{
tim[i]=st[i]*1.0*tim[i+1]/st[i+1];
if(tim[i]*st[i+1]/tim[i+1]!=st[i]) tim[i]++;
}
sum+=tim[i];
//ceshi
//printf("%d %.2f %lld\n",st[i],v[i],tim[i]);
}
}
printf("Case #%d: %lld\n",k,sum);
}
}