64,654
社区成员
发帖
与我相关
我的任务
分享
template<class T> // T: 重载 ostream& (<<)(ostream&, T&), 重载 long (-)(T&, T&)
bool So(T* t, size_t count)
{
if (!t || !count )
return false;
long sign = t[1] - t[0];
sign = sign == 1 ? 1 : sign == -1 ? -1 : 0;
cout << t[0];
for (size_t n = 1; n < count; n++)
{
long dif = t[n] - t[n-1];
if (!dif || dif != sign)
{
if (sign)
cout << " - " << t[n-1];
sign = dif == 1 ? 1 : dif == -1 ? -1 : 0;
if (!sign)
cout << ", " << t[n];
}
}
if (sign)
cout << " - " << t[count-1];
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
long a[] = {1, 2, 3, 4, 7, 8, 8, 9, 10, 11, 12, 67, 55, 54, 53, 52, 50, 99, 85, 10, 9, 8, 7,7 ,7, 7 };
So(a, _countof(a));
return 0;
}