SDUT 2020 Autumn Team Contest---31(for 18&&19)--- C - Car

【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);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回文串是指正着读和倒着读都一样的字符串。例如,"level"、"racecar"、"madam" 都是回文串。 判断一个字符串是否为回文串,可以使用双指针法。定义两个指针,一个指向字符串的开头,一个指向字符串的结尾,然后分别向中间移动,比较两个指针所指向的字符是否相同。如果相同,继续移动;如果不同,说明不是回文串。 具体实现可以参考以下代码: ```c #include <stdio.h> #include <string.h> int isPalindrome(char *str) { int len = strlen(str); int left = , right = len - 1; while (left < right) { if (str[left] != str[right]) { return ; } left++; right--; } return 1; } int main() { char str[100]; printf("请输入一个字符串:"); scanf("%s", str); if (isPalindrome(str)) { printf("%s 是回文串\n", str); } else { printf("%s 不是回文串\n", str); } return ; } ``` 在上面的代码中,isPalindrome 函数用于判断一个字符串是否为回文串。该函数首先获取字符串的长度 len,然后定义两个指针 left 和 right,分别指向字符串的开头和结尾。接着,使用 while 循环,当 left 小于 right 时,比较 str[left] 和 str[right] 是否相同,如果不同,说明不是回文串,返回 ;如果相同,继续移动指针。当 left 大于等于 right 时,说明已经比较完了整个字符串,返回 1,表示是回文串。 在 main 函数中,首先使用 scanf 函数获取用户输入的字符串,然后调用 isPalindrome 函数判断该字符串是否为回文串,并输出结果。 希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值