#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string strTmp("0 0 2 4");
string strTmp_1("0 2 2 2");
string strTmp_2("0 4 2 2");
string strTmp_3("8 8 2 2");
vector<string> input;
input.push_back(strTmp);
input.push_back(strTmp_1);
input.push_back(strTmp_2);
input.push_back(strTmp_3);
vector<vector<int> > vResult;
for (int i = 0; i < input.size(); i++)
{
string strSplit = input[i];
vector<int> vNumSplit;
while (!strSplit.empty())
{
int nIndex = strSplit.find(" ");
string strNum;
if (nIndex != -1)
{
strNum = strSplit.substr(0, nIndex);
strSplit = strSplit.substr(nIndex + 1, strSplit.length() - strNum.length() - 1);
}
else
{
strNum = strSplit;
strSplit.clear();
}
int nNum = atoi(strNum.c_str());
vNumSplit.push_back(nNum);
}
for (int i = 0; i < vNumSplit.size() - 1; i++)
{
if (vNumSplit[i] == vNumSplit[i + 1])
{
vNumSplit[i] += vNumSplit[i + 1];
vNumSplit[i + 1] = 0;
}
else
continue;
}
int k = 0;
int nCount = 0;
while(k < vNumSplit.size())
{
if (nCount >= vNumSplit.size())
break;
if (vNumSplit[k] == 0)
{
for (int j = k; j < vNumSplit.size() - 1; j++)
{
vNumSplit[j] = vNumSplit[j + 1];
}
vNumSplit[vNumSplit.size() - 1] = 0;
}
if (vNumSplit[k] == 0)
{
nCount++;
continue;
}
else
{
nCount++;
k++;
}
}
vResult.push_back(vNumSplit);
}
for (int i = 0; i < vResult.size(); i++)
{
for (int j = 0; j < vResult[i].size() - 1; j++)
{
cout << vResult[i][j] << " ";
}
cout << vResult[i][vResult[i].size() - 1] << endl;
}
system("pause");
return 0;
}