蓝桥杯-错误票据

蓝桥杯简单题。

题目背景

某涉密单位下发了某种票据,并要在年终全部收回。

题目描述

每张票据有唯一的 ID 号,全年所有票据的 ID 号是连续的,但 ID 的开始数码是随机选定的。因为工作人员疏忽,在录入 ID 号的时候发生了一处错误,造成了某个 ID 断号,另外一个 ID 重号。

你的任务是通过编程,找出断号的 ID 和重号的 ID。

数据保证断号不可能发生在最大和最小号。

输入格式

一个整数 N(N

输出格式

要求程序首先输入要求程序输出 11 行,含两个整数 mm,nn,用空格分隔,其中,mm 表示断号 ID,nn 表示重号 ID。

输入输出样例

输入 #1

2
5 6 8 11 9
10 12 9

输出 #1

7 9

输入 #2

6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

输出 #2

10

思路一:求最大最小值+字符统计
思路二:排序+遍历
题目有意思的地方在于输入数据的处理,详情看代码。

c++的sstream输入流处理读入数据+排序遍历

#include
#include
#include
#include
using namespace std;
const int N = 10010;
int n;
int a[N];
int main(){
    int cnt;
    cin>>cnt;
    string line;
    getline(cin,line);//用于忽略第一行的回车
    while(cnt--){
        getline(cin,line);//字符串里面有空格
        stringstream ssin(line);//可以等价位sscanf
        while(ssin>>a[n]) n++;//这里ssin等价为cin
    }
    sort(a,a+n);
    int res1,res2;
    for(int i=2;i

代码2:

//思路 找出最大值和最小值+出现字符统计
#include
#include
#include
using namespace std;
const int N  = 100010;
const int INF = 0x3f3f3f3f;
int st[N];
int main(){
    //输出数据的行数
    int n;
    cin>>n;
    int temp;
    //初始化要找的最大值和最小值
    int minv=INF,maxv=-INF;
    //ans用于存储断号 res存储重号
    int res,ans;
    while(cin>>temp){
        if(tempmaxv) maxv = temp;
        st[temp]++;
    }
    for(int i=minv;i

Original: https://blog.csdn.net/m0_62404686/article/details/128151509
Author: 算法给的安全感
Title: 蓝桥杯-错误票据

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/724160/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球