AOJ 559 消失的数字 不知道为什么会超时。。

来源:互联网 发布:mysql查询取第一条 编辑:程序博客网 时间:2024/05/15 10:01
丢失的数字
Time Limit: 1000 ms   Memory Limit: 64 MB
Total Submission: 1488   Submission Accepted: 254
Description
有N个数字是来自一个长度为N+1的连续整数序列,但是给你的并不是有序的,请你帮忙找出来是缺失的那个数字是在序列的两边还是中间

Input
有多组测试数据,每组测试数据包括2行,第一行包括一个整数N(0<N<10000),第二行包括N个整数
以N为0结束

Output
每组测试数据输出结果:
中间缺失输出M,两边缺失输出S

Sample Input
OriginalTransformed
52 3 7 5 653 4 2 5 60

Sample Output
我觉得很简单的一个题目,先用sort排序,然后看看首位相加除以二的值是否和中间位置的值相等,如果不相等,就肯定缺失中间的数了,问题是为什么会超时。。留给以后的自己看看吧。。
   
#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){int m,temp,k,t;cin>>m;vector<int>vec;t=0;while(m!=0){ t++;for(int i=1;i<=m;i++){ cin>>temp; vec.push_back(temp);}     vector<int>::iterator iter=vec.begin();    vector<int>::iterator result;sort(vec.begin(),vec.end());k=(vec[0]+vec[vec.size()-1])/2;if(k!=vec[vec.size()/2]) cout<<"Case "<<t<<":"<<endl<<"M"<<endl;else cout<<"Case "<<t<<":"<<endl<<"S"<<endl;cin>>m;vec.clear();}return 0;}


0 0
原创粉丝点击