某涉密单位下发了某种票据,并要在年终全部收回。
每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。
因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
你的任务是通过编程,找出断号的ID和重号的ID。
假设断号不可能发生在最大和最小号。
要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000),请注意行内和行末可能有多余的空格,你的程序需要能处理这些空格。
每个整数代表一个ID号。
要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID
#include<cstdio> #include<iostream> #include<sstream> #include<string> #include<cstring> #include<algorithm> using namespace std; int num[100005]; int n; string s; int toInt(string word) { int len=word.length(); int x=0; for(int i=0;i<len;i++) { x*=10; x=(x+word[i]-‘0‘); } return x; } int main() { int mx=-1,mn=100005; scanf("%d",&n); scanf("%*c"); for(int i=0;i<n;i++) { getline(cin,s); stringstream line; line<<s; while(line) { string word; line>>word; int x=toInt(word); mn=min(x,mn); mx=max(x,mx); num[x]++; } } int a,b; for(int i=mn;i<=mx;i++) { if(num[i]==0) a=i; if(num[i]==2) b=i; } printf("%d %d\n",a,b); }
c 的gets
#include<cstdio> #include<cstring> #define Min(a,b) (a>b)?b:a #define Max(a,b) (a>b)?a:b using namespace std; char x[1000]; int num[100005]; int main() { int row; scanf("%d",&row); getchar(); int maxn=-1; int minn=100005; for(int y=0;y<row;y++) { gets(x); int i=0; int e=0; while(true) { if(x[i]==‘ ‘||x[i]==‘\0‘) { minn=Min(e,minn); maxn=Max(e,maxn); num[e]++; e=0; if(x[i]==‘\0‘) break; } else { e*=10; e+=x[i]-‘0‘; } i++; } } int a,b; for(int i=minn;i<=maxn;i++) { if(num[i]==0) { a=i; } if(num[i]==2) { b=i; } } printf("%d %d\n",a,b); return 0; }
原文:http://www.cnblogs.com/program-ccc/p/5275403.html