#include "stdafx.h" #include "iostream" #include <set> #include <vector> using namespace std; void GetMaxMin(const set<int> & setNum, int &nMax, int &nMin) { nMin = 13; nMax = 1; set<int>::const_iterator iter = setNum.begin(); for (; iter != setNum.end();iter ++) { if(*iter < nMin) nMin = *iter; if(*iter > nMax) nMax = *iter; } } int Del0Num(set<int> & setNum,const vector<int>& data) { int Num0 = 0; vector<int >::const_iterator iter = data.begin(); for (; iter != data.end();iter ++) { if(*iter != 0) setNum.insert(*iter); else Num0++; } return Num0; } bool IsContinuous(vector <int> data) { int nMax = 0, nMin = 0; set<int> setNum; int num0 = Del0Num(setNum, data); if (num0 + setNum.size() < data.size()) { return false; } GetMaxMin(setNum,nMax,nMin); return nMax - nMin <=(int) (data.size())-1; } int _tmain(int argc, _TCHAR* argv[]) { vector<int> vec; for (int i = 0; i < 5; i ++) { int temp; cin>>temp; vec.push_back(temp); } cout<<IsContinuous(vec)<<endl; return 0; }
原文:http://blog.csdn.net/gaoxiangky/article/details/23203605