最长顺子;输入很多个整数(1<=数值<=13),返回其中可能组成的 最长的一个顺子(顺子中数的个数代表顺的长度); 其中数字1也可以当作14用;顺子包括单顺\双顺\3顺;单顺的定义是连续5个及以上连续的数,比如1,2,3,4,5、3,4,5,6,7,8和10,11,12,13,1等;双顺的定义是连续3个及以上连续的对(对:两个相同的数被称为对),比如1,1,2,2,3,3、4,4,5,5,6,6,7,7和12,12,13,13,1,1等;3顺的定义是连续2个及以上连续的3张(3张:3个相同的数被称为3张),比如1,1,1,2,2,2、3,3,3,4,4,4,5,5,5,6,6,6和13,13,13,1,1,1等等;比如:输入数[1,5,2,3,4,4,5,9,6,7,2,3,3,4], 输出数组[2,2,3,3,4,4,5,5]
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stdlib.h>
#define N 2
#define MAX_LEN 128
using namespace std;
void PrintString(vector<int>& vec)
{
cout<<"[test3]'s result is : "<<vec.size()<<endl;
if(0 == vec.size())
{
cout<<"Not find match ShunZi!"<<endl;
return;
}
for(int i = 0;i < vec.size();i++)
{
int itemp = vec[i];
if(14 == itemp)
cout<<itemp - 13<<" ";
else
cout<<itemp<<" ";
}
cout<<endl;
}
bool SplitString(const string& src,const string& separator,vector<int>& dest)
{
string str = src;
string substring;
string::size_type start = 0,index;
int iver = 0;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
iver = atoi(substring.c_str());
dest.push_back(iver);
iver = 0;
start = str.find_first_not_of(separator,index);
if (start == string::npos)
return false;
}
}while(index != string::npos);
substring = str.substr(start);
iver = atoi(substring.c_str());
dest.push_back(iver);
iver = 0;
return true;
}
void TrimString(string& str)
{
if(str.empty())
return;
str.erase(0,str.find_first_not_of("\t"));
str.erase(str.find_last_not_of("\t") + 1);
str.erase(0,str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
}
void BubbleSort(int *array,int length)
{
int inner = 0, outer = 0;
int median = 0;
int flag = 1;
if(NULL == array || 0 == length)
return;
for(outer = length-1; outer >= 1 && flag; outer --){
flag = 0;
for(inner = 0; inner < outer; inner ++){
if(array[inner] > array[inner + 1]){
median = array[inner];
array[inner] = array[inner + 1];
array[inner + 1] = median;
if(flag == 0)
flag = 1;
}
}
}
}
void GetLongerVec(vector<int>& tempvec,vector<int>& vec,int itype,int steplen)
{
int icount = tempvec.size();
if(itype <= icount/steplen && icount > vec.size())
{
vec.clear();
for(int j = 0;j < icount;j++)
vec.push_back(tempvec[j]);
}
}
void GetVec(int (*array2)[N],int length,vector<int>& vec,int steplen)
{
if(NULL == array2 || 0 == length || 0 == steplen)
return;
vector<int> tempvec;
int index = 0;
int icount = 0;
for(int i = index;i < length;)
{
if(i == index)
{
if(steplen <= array2[i][1])
{
for(int m = 0;m < steplen;m++)
tempvec.push_back(array2[i][0]);
}
else
{
index += 1;
i = index;
continue;
}
}
if(steplen <= array2[i][1] && steplen <= array2[i+1][1] && array2[i][0] + 1 == array2[i+1][0])
{
for(int m = 0;m < steplen;m++)
tempvec.push_back(array2[i+1][0]);
i++;
}
else
{
index += 1;
i = index;
if(1 == steplen)
GetLongerVec(tempvec,vec,5,steplen);
else if(2 == steplen)
GetLongerVec(tempvec,vec,3,steplen);
else if(3 == steplen)
GetLongerVec(tempvec,vec,2,steplen);
else
return;
tempvec.clear();
}
}
}
int CompareVec(int ivec1,int ivec2,int ivec3)
{
return ivec1 > ivec2 ? (ivec1 > ivec3 ? 1 : 3):(ivec2 > ivec3 ? 2 : 3);
}
void GetArray2(int* array,int length,vector<int>& vec)
{
if(NULL == array || 0 == length)
return;
int array2[length][N];
int index = 0;
int icount = 0;
bool flag = false;
for(int i = index;i < length;i++)
{
if(!flag)
{
array2[icount][0] = array[i];
array2[icount][1] = 1;
}
if(array[i] == array[i+1])
{
flag = true;
array2[icount][1] += 1;
continue;
}
else
{
index = i + 1;
icount++;
flag = false;
}
}
vector<int> vec1;
GetVec(array2,icount,vec1,1);
vector<int> vec2;
GetVec(array2,icount,vec2,2);
vector<int> vec3;
GetVec(array2,icount,vec3,3);
int ivec1 = vec1.size();
int ivec2 = vec2.size();
int ivec3 = vec3.size();
int icmpvec = CompareVec(ivec1,ivec2,ivec3);
if(1 == icmpvec)
{
vec.clear();
for(int j = 0;j < vec1.size();j++)
vec.push_back(vec1[j]);
return;
}
else if(2 == icmpvec)
{
vec.clear();
for(int j = 0;j < vec2.size();j++)
vec.push_back(vec2[j]);
return;
}
else if(3 == icmpvec)
{
vec.clear();
for(int j = 0;j < vec3.size();j++)
vec.push_back(vec3[j]);
return;
}
else
return;
}
void GetArray(int *array,int length)
{
if(NULL == array || 0 == length)
return;
int array0[length];
vector<int> vec1,vec2;
for(int i = 0;i < length;i++)
{
if(1 == array[i])
array0[i] = array[i] + 13;
else
array0[i] = array[i];
}
BubbleSort(array,length);
GetArray2(array,length,vec1);
if(0 < sizeof(array0)/sizeof(int))
{
BubbleSort(array0,length);
GetArray2(array0,length,vec2);
}
if(vec1.size() > vec2.size())
PrintString(vec1);
else
PrintString(vec2);
}
void test3(const string& str)
{
if(str.empty())
return;
vector <int> dest;
if(!SplitString(str,",",dest))
{
cout<<"Your numbers ["<<str<<"] have format error!"<<endl;
return;
}
int sizev = dest.size();
int n[sizev],m[sizev];
int icount = 0;
for(int i = 0;i < sizev;i++)
{
if(1 > dest[i] || 13 < dest[i])
{
m[icount] = dest[i];
icount++;
}
else
n[i] = dest[i];
}
if(0 < icount)
cout<<"The following Numbers is not correct in your inputing numbers : "<<endl;
for(int j = 0;j < icount;j++)
{
cout<<m[j]<<" ";
}
cout<<endl;
GetArray(n,sizev);
}
int main()
{
try
{
char buffer[MAX_LEN];
loop:
cout<<"Please enter some number(1<=n<=13),and use \", \"separated : "<<endl;
cin.getline(buffer,MAX_LEN);
string str(buffer);
TrimString(str);
if(str.empty())
{
cout<<"Please input some number again!"<<endl;
goto loop;
}
test3(str);
}
catch(...)
{
cout<<"[test3] has a error!"<<endl;
}
return 0;
}
希望大家多提意见