[蓝桥杯]错误票据---stringstream应用举例
題目描述
某涉密單位下發(fā)了某種票據(jù),并要在年終全部收回。
每張票據(jù)有唯一的ID號(hào)。全年所有票據(jù)的ID號(hào)是連續(xù)的,但I(xiàn)D的開始數(shù)碼是隨機(jī)選定的。
因?yàn)楣ぷ魅藛T疏忽,在錄入ID號(hào)的時(shí)候發(fā)生了一處錯(cuò)誤,造成了某個(gè)ID斷號(hào),另外一個(gè)ID重號(hào)。
你的任務(wù)是通過編程,找出斷號(hào)的ID和重號(hào)的ID。
假設(shè)斷號(hào)不可能發(fā)生在最大和最小號(hào)。
輸入
要求程序首先輸入一個(gè)整數(shù)N(N<100)表示后面數(shù)據(jù)行數(shù)。接著讀入N行數(shù)據(jù)。
每行數(shù)據(jù)長(zhǎng)度不等,是用空格分開的若干個(gè)(不大于100個(gè))正整數(shù)(不大于100000)
請(qǐng)注意行內(nèi)和行末可能有多余的空格,你的程序需要能處理這些空格。
每個(gè)整數(shù)代表一個(gè)ID號(hào)。
輸出
要求程序輸出1行,含兩個(gè)整數(shù)m n,用空格分隔。
其中,m表示斷號(hào)ID,n表示重號(hào)ID
樣例輸入
2
5 6 8 11 9
10 12 9
樣例輸出
7 9
此題注意點(diǎn)
核心代碼:
1.getline(cin,line)//用來輸入一行字符
2.stringstream ss(line);
while (ss>>a[k]) k++;//用于拆分
代碼如下:
#include <iostream> #include <sstream> #include <algorithm> using namespace std; const int N = 100010; int a[N];int main() {int cnt = 0;string line;cin >> cnt;//注意后面有回車,所以要用getline把回車抹掉getline(cin, line);int k = 0;while (cnt--) {getline(cin, line);stringstream ss(line);while (ss >> a[k])k++;}sort(a, a + k);int b1 = 0, b2 = 0;for (int i = 1; i < k; i++) {if (a[i] == a[i - 1])b1 = a[i];if (a[i] == a[i - 1] + 2)b2 = a[i - 1] + 1;}cout << b2 << " " << b1 << endl;return 0; }為什么下面的代碼只能拿50分,ac不了呢???
#include <iostream> #include <sstream> #include <algorithm> using namespace std; const int N = 100010; int a[N];int main() {int cnt = 0;string line;cin >> cnt;getchar();int k = 0;while (cnt--) {getline(cin, line);stringstream ss(line);while (ss >> a[k])k++;}sort(a, a + k);int b1 = 0, b2 = 0;for (int i = 1; i < k; i++) {if (a[i] == a[i - 1])b1 = a[i];if (a[i] == a[i - 1] + 2)b2 = a[i - 1] + 1;}cout << b2 << " " << b1 << endl;return 0; }看完這個(gè),我相信你就知道了。
總結(jié)
以上是生活随笔為你收集整理的[蓝桥杯]错误票据---stringstream应用举例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给大家用手机更新赚钱模式吧给大家用手机更
- 下一篇: [蓝桥杯]回形取数-方向向量+模拟