|
楼主 |
发表于 2019-2-10 17:34:37
|
显示全部楼层
我后来又稍微改了改大佬的代码#include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
vector<int> Add_left(vector<int> v, int bi, int ei)
{
int temp=0;
for (int i = bi; i < ei; i++)
{
temp += v[i];
v[i] = 0;
}
v[bi] = temp;
return v;
}
vector<int> Add_Samenum(vector<int>& v,int bi,int ei)
{
vector<int> v2,v3,v4;
int temp;
for(int i=bi;i<ei;i++){
if(v[i]!=0)
v4.push_back(v[i]);
}
for (int i = bi; i < ei; i++)
{
if(v[i]!=0){
if (v[i] == v[i + 1])
{
temp = v[i] + v[i + 1];
i++;
}
else
temp = v[i];
v2.push_back(temp);
}
else{
v3.push_back(0);
}
}
while (v2.size() < v4.size())
{
v2.push_back(0);
}
v.erase(v.begin()+bi,v.begin()+ei);
for(int i=0;i<v2.size();i++){
v.insert(v.begin()+bi+i,v2[i]);
}
for(int i=0;i<v3.size();i++){
v.insert(v.begin()+bi+v2.size(),v3[i]);
}
return v;
}
int main()
{
vector<int> v1 = { 1,1,2,2,3,3,4,4,5,5,6,6 };
vector<int> v2 = { 1,1,2,2,3,3,4,4,5,5,6,6 };
v1 = Add_left(v1, 4, 6);
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
std::cout<<std::endl;
v2 = Add_Samenum(v2,0,4);
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
v2 = Add_Samenum(v2,4,8);
std::cout<<std::endl;
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
std::cout<<std::endl;
v2 = Add_Samenum(v2,8,12);
std::cout<<std::endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
std::cout<<std::endl;
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
system("pause");
return 0;
}
这样就能实现目标了,太感谢了! |
|