无聊的题解之第K名

最近我在计蒜客上面做题,发现了一大堆简单的不得了的题目,这个是专辑(无聊题解)的第一张,直接进入正题:

没有传送门!

直接看题目:

时间限制:1000ms,内存限制:131072K
题目背景:
刚举行的万米长跑活动中,有 N 个人跑完了全程,所用的时间都不相同。颁奖时为了增加趣味性,随机抽了一个数K,要奖励第 K 名一双跑鞋。

现在组委会给你 N 个人的姓名、成绩(用时,单位是秒),请你编程快速输出第 K 名的姓名。
输入格式:
第一行:22 个整数 N 和 K。

下面 N 行:每行第 11 个是字符串表示姓名;第 22 个是个整数,表示这个人跑完的使用时间。
输出格式:
一行,第 K 名的姓名。
数据范围:
1 ≤ K ≤ N ≤ 100。
补充:输出时每行末尾的多余空格,不影响答案正确性
样例输入:
5 3
lisi 2306
zhangsan 3013
wangwu 3189
suantoujun 4012
zhaoliu 2601
样例输出:
zhangsan

看到这个题目,不要蒙圈,看题!

从题目中,我们知道这是一个跑步比赛和一个结构体排序,很简单,很鸡肋

首先,要理解的是跑步比赛的评分规则(用时越少,排名越靠前)。

所以,主体就出来了:

先输入头文件

#include<bits/stdc++.h>

然后把变量定好,就成了这样:

#include<bits/stdc++.h> //万能头文件
#define MAXN 105        //define一下
using namespace std;    //必须写的
struct people{          //结构体定义
    string name;
    int time;
}a[MAXN];
bool cmp(people a, people b){ //cmp(比较函数)这个是根据成绩从小到大排序,所以需要它
    return a.time < b.time;
}
int n, k;              //题目让设的变量

由于要排序,最方便的就属sort了,先上代码,文章最后讲解!

#include<bits/stdc++.h> //万能头文件
#define MAXN 105        //define一下
using namespace std;    //必须写的
struct people{          //结构体定义
    string name;
    int time;
}a[MAXN];
bool cmp(people a, people b){ //cmp(比较函数)这个是根据成绩从小到大排序,所以需要它
    return a.time < b.time;
}
int n, k;              //题目让设的变量
int main(void){        //主函数
    cin >> n >> k;     //来一波输入
    for(int i = 1;i <= n;i ++){
        cin >> a[i].name >> a[i].time;
    }
    sort(a + 1, a + n + 1, cmp); //sort函数的使用
}

题目上面写着:现在组委会给你 N 个人的姓名、成绩(用时,单位是秒),请你编程快速输出第 K 名的姓名。

也就是说,要把结构体中第K个元素中的姓名输出出来,上代码:

#include<bits/stdc++.h> //万能头文件
#define MAXN 105        //define一下
using namespace std;    //必须写的
struct people{          //结构体定义
    string name;
    int time;
}a[MAXN];
bool cmp(people a, people b){ //cmp(比较函数)这个是根据成绩从小到大排序,所以需要它
    return a.time < b.time;
}
int n, k;              //题目让设的变量
int main(void){        //主函数
    cin >> n >> k;     //来一波输入
    for(int i = 1;i <= n;i ++){
        cin >> a[i].name >> a[i].time;
    }
    sort(a + 1, a + n + 1, cmp); //sort函数的使用
    cout << a[k].name;           //输出结构体中第K个元素中的姓名
    return 0;                    //加上Return是个好习惯
}

看看程序能不能正常运行!

看到了第二个exe文件吗?就表示能正常运行了。

看看,样例输入与样例输出是否相等。

PS D:\Programs\Programs> cd "d:\Programs\Programs\" ; if ($?) { g++ diKming.cpp -o diKming } ; if ($?) { .\diKming }
5 3
lisi 2306
zhangsan 3013
wangwu 3189
suantoujun 4012
zhaoliu 2601
zhangsan
PS D:\Programs\Programs> 

 与样例输出相等,提交!

过了!

讲解:

1、bits/stdc++.h

这个头文件是万能头文件(包含了差不多的头文件,为什么要叫bits/stdc++.h?因为它是在bits文件夹下的头文件),这是它的源代码:

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

 2、sort函数。

它的解释是这样的:

function template
<algorithm>
std::sort
default (1)	
template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);
custom (2)	
template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Sort elements in range
Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).

这个是它的函数要用到的东西的解释:

Parameters
first, last
Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.
comp
Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

这个就是第K名的题解了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Antarctic Airlines

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值