标签:
某涉密单位下发了某种票据,并要在年终全部收回。
每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。
因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
你的任务是通过编程,找出断号的ID和重号的ID。
假设断号不可能发生在最大和最小号。
要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000),请注意行内和行末可能有多余的空格,你的程序需要能处理这些空格。
每个整数代表一个ID号。
要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID
while(1)
{
cin>>a; //读入编号
c=getchar();//读入空格或是回车
arr.push_back(a);
if(c==‘\n‘)
{
d1++; //一个回车已读一行
if(d1==d) //读满给定行数停止
{
break;
}
}
}
简单的实现了读入不规则数据。
下面就是找 重号和断号了。
int xxx()
{
int xx=0;
for(it=arr.begin()+1;it!=arr.end();it++)
{
if(*it-*(it-1)==2) //<a,b> b-a==2 a<c<b
{
cout<<*it-1<<" "; // c=b-1
}
if(*it-*(it-1)==0) //<a,b> a-b==0 a==b
{
cout<<*it;
}
}
return 0;
}
最后是我的源码,用简单的数据测试通过。
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> arr;
vector<int>::iterator it;
int d=0,d1=0;
int xxx()
{
int xx=0;
for(it=arr.begin()+1;it!=arr.end();it++)
{
if(*it-*(it-1)==2) //<a,b> b-a==2 a<c<b
{
cout<<*it-1<<" "; // c=b-1
}
if(*it-*(it-1)==0) //<a,b> a-b==0 a==b
{
cout<<*it;
}
}
return 0;
}
int main()
{
int i=0;
int a=0;
char c=‘ ‘;
cin>>d;
while(1)
{
cin>>a; //读入编号
c=getchar();//读入空格或是回车
arr.push_back(a);
if(c==‘\n‘)
{
d1++; //一个回车已读一行
if(d1==d) //读满给定行数停止
{
break;
}
}
}
sort(arr.begin(),arr.end());
xxx();
return 0;
}
2015-03-15
标签:
原文地址:http://www.cnblogs.com/sundy-lee/p/4340321.html