#C++初学记录(算法4)

2023-05-16

A - Serval and Bus
It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.

Serval will go to the bus station at time t, and there are n bus routes which stop at this station. For the i-th bus route, the first bus arrives at time si minutes, and each bus of this route comes di minutes later than the previous one.

As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.

Input
The first line contains two space-separated integers n and t (1≤n≤100, 1≤t≤105) — the number of bus routes and the time Serval goes to the station.

Each of the next n lines contains two space-separated integers si and di (1≤si,di≤105) — the time when the first bus of this route arrives and the interval between two buses of this route.

Output
Print one number — what bus route Serval will use. If there are several possible answers, you can print any of them.

Examples
Input
2 2
6 4
9 5
Output
1
Input
5 5
3 3
2 5
5 6
4 9
6 1
Output
3
Input
3 7
2 2
2 3
2 4
Output
1
正确代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long s[105],d[105], maxx = INT_MAX, flag = 0, n, t,temp,temp1;
    scanf("%lld%lld", &n, &t);
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld", &s[i], &d[i]);
        if(s[i] >= t && maxx>s[i]){
            maxx = s[i];
            if(maxx == t){
                flag = 1;
                temp1 = i;
            }
            temp = i;
        }
    }
    //cout << maxx <<" " <<temp << endl;
    if(maxx != INT_MAX &&flag == 1){
        printf("%lld\n", temp1);
        return 0;
    }
    for(int i = 1; i <= n; i++){
        while(maxx > s[i] && s[i] < t){
            s[i] += d[i];
        }
    }
//    for(int i = 1; i <= n; i++){
//        printf("%lld ", s[i]);
//    }
//    printf("\n");
    for(int i = 1; i <= n; i++){
        if(maxx > s[i]){
            maxx = s[i];
            temp = i;
        }
    }
    printf("%lld\n", temp);
}

代码理解
nt的意义分别是公交车的数量和主人公到达的时间,每一路段的公交车都和现实生活中一样由多辆公交车有时段间隔的进行行驶,之后n个循环输入的s和d是每段首辆公交车到主人公站点的时间和下一辆车到达时间的间隔,maxx里面存储的是最短时间,即公交车不管是首班还是中途哪班只要间隔和主人公到达时间最短则会保存在maxx变量里面。因为要输入n个数据进入s和d数组,所以maxx里的数据会不停的改变。这里有几个极限判断,即是车到达时间和主人公到达时间相同时,则会跳出循环直接输出车次。temp1是记录时间间隔最短的车次、到最后一个数据输入完毕,计算机的运行也到此结束,因此输出step1,结束程序。
错误及调试
自己进行编写代码的时候运行结果正确但是OJ测试一直WRONG,自己曾百思不得其解,之后参考了AC代码并进行了对比发现了错误。

int tex(int a,int b)
{
    if(a<t)
    {
        a=a+b;
        tex(a,b);
    }
    else if(a>=t)
    {
        return a-t;
    }
}

此判断函数是第一次WRONG的判断函数,首要思路是将全部路线的车次到达时间通过数组h进行记录下来,数组的位次为车辆的位次,数组的数据为车辆到达的时间和主人公到达车站时间的间隔,通过这个函数将所有数据进行统一判断是当时编写的主要思路,有一个小细节当时没有考虑到,即是题意规定如果有相同最小时间间隔的车辆进入车站,即同时进入车站,则主人公会随机选一辆车进入,换言之就是第几辆车的输出是由编写代码的人决定的,因此,在判断出时间间隔为0的时候可以直接进行输出结束程序继续运行,这样可以节省大部分的运行时间。

以下是错误部分源代码:

    for(int i=1;i<=n;i++)
    {
        if(h[i]>h[i-1])
        low=i-1;
    }
    cout<<low<<"\n";
    return 0;
}

在进行判断时,只进行了数组前后的判断,即h1和h2,h2和h3判断,从而或略了对全局的判断,因此我将代码进行修改:

    int minn=h[1];
    for(int i=1;i<=n;i++)
    {
        if(minn>h[i])
        {
            low=i;
            minn=h[i];
        }
    }

这样就解决了程序判断不准确的问题,并且完成程序输出得到正确答案。

转载于:https://www.cnblogs.com/xiaofengqaq/p/10725501.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

#C++初学记录(算法4) 的相关文章

  • linux分区btrfs,系统基础之Btrfs文件系统详解

    btrfs文件系统 技术预览版 centos7 描述 Btrfs B tree Butter FS Better fs GPL授权 Orale 2007 写实复制特性 Cow cp reflink 只能在btrfs文件系统中使用 想取代ex
  • UITableView 总结

    UITableView 1 重用代理 64 interface ViewController UIViewController lt UITableViewDelegate UITableViewDataSource gt 2 定义 tab
  • 【转】ASP.Net2.0 AJAX Extensions 1.0的安装

    文章出处 xff1a DIY部落 http www diybl com course 4 webprogram asp net asp netshl 2008828 138230 html 以前一直没有去应用ajax xff0c 这2天用了
  • pip国内源

    pip 国内源 xff1a 清华 xff1a https pypi tuna tsinghua edu cn simple 阿里云 xff1a http mirrors aliyun com pypi simple 中国科技大学 https
  • 视频教程-C语言入门篇-C/C++

    C语言入门篇 23年C 43 43 语言编程经验 xff0c 经历过多个行业的开发项目包括网络安全 xff0c 网络游戏 xff0c 通信行业等等 xff0c 多年的摸爬滚打使自身具备了深厚的开发实力和实战经验 王健伟 98 00 立即订阅
  • ubuntu - 如何以root身份使用图形界面管理文件?

    nautilus 是gnome的文件管理器 xff0c 但是如果不是root账号下 xff0c 权限受限 xff0c 我们可以通过以下方式以root权限使用 xff01 一 xff0c 快捷键 ctrl 43 alt 43 t 调出shel
  • debian添加删除用户

    debian添加删除用户 增加普通用户 命令 xff1a adduser abc passwd abc exit 用abc登录 etc passwd中保存了用户信息 LINUX创建用户的命令 useradd g test d home te
  • MongoDB——副本集的同步、选举和回滚

    1 同步 复制用于在多台服务器之间备份数据 MongoDB的复制功能是使用操作日志oplog实现的 xff0c 操作日志包含了主节点的每一次写操作 oplog是主节点的locl数据库中的一个固定集合 备份节点通过查询这个集合就可以知道需要进
  • ftp (文件传输协议)

    ftp xff08 文件传输协议 xff09 锁定 本词条由 科普中国 百科科学词条编写与应用工作项目 审核 FTP 是File Transfer Protocol xff08 文件传输协议 xff09 的英文简称 xff0c 而中文简称为
  • SQL Server 中的 JSON 数据

    下面是 JSON 文本的示例 34 name 34 34 John 34 34 skills 34 34 SQL 34 34 C 34 34 Azure 34 34 name 34 34 Jane 34 34 surname 34 34 D
  • 去除地址栏带#的问题

    vue cil搭建的项目 第一步 xff1a 找到目录下router js文件 第二步 xff1a 在new Router xff08 routes xff1a xff09 中添加mode属性 xff0c 默认mode是hash expor
  • 如何给自己的Python项目制作安装包

    Packaging Python Projects 本教程将指导您如何打包一个简单的Python项目 它将向您展示如何添加必要的文件和结构来创建包 xff0c 如何构建包以及如何将其上载到Python包索引 A simple project
  • linux安装解压工具gzip,笔记6 压缩工具(gzip,bzip2,xz,zip,tar)。

    压缩打包 常见的压缩文件 windows rar zip 7z Linux zip gz bz2 xz tar gz tar bz2 tar xz gzip压缩工具 不能压缩目录 gzip压缩后边直接跟文件名就可以 xff0c gunzip
  • 洛谷 P3367 【模板】并查集

    P3367 模板 并查集 题目描述 如题 xff0c 现在有一个并查集 xff0c 你需要完成合并和查询操作 输入输出格式 输入格式 xff1a 第一行包含两个整数N M xff0c 表示共有N个元素和M个操作 接下来M行 xff0c 每行
  • js计算器(正则)

    lt doctype html gt lt html gt lt head gt lt meta charset 61 34 utf 8 34 gt lt title gt 我的计算器 lt title gt lt style gt mar
  • MySQL 分组后取每组前N条数据

    与oracle的 rownumber over partition by xxx order by xxx 语句类似 xff0c 即 xff1a 对表分组后排序 创建测试emp表 DROP TABLE IF EXISTS emp CREAT
  • archlinux 安装搜狗输入法

    安装可能需要 archlinuxcn 的源 xff0c 我这里已经配置好了 一 安装 fcitx fcitx configtool fcitx im pacman S fcitx fcitx configtool fcitx im 二 在
  • MongoDB——JavaAPI详解

    环境配置 引入MongoDB驱动 xff1a span class token tag span class token tag span class token punctuation lt span dependency span sp
  • 练习题||并发编程

    线程 进程 队列 IO多路模型 操作系统工作原理介绍 线程 进程演化史 特点 区别 互斥锁 信号 事件 join GIL 进程间通信 管道 队列 生产者消息者模型 异步模型 IO多路复用模型 select poll epoll 高性能IO模
  • luogu P2078 朋友

    题目背景 小明在A公司工作 xff0c 小红在B公司工作 题目描述 这两个公司的员工有一个特点 xff1a 一个公司的员工都是同性 A公司有N名员工 xff0c 其中有P对朋友关系 B公司有M名员工 xff0c 其中有Q对朋友关系 朋友的朋

随机推荐