![]() |
#2
moox2017-12-31 18:04
|

#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<int> add(vector<int> &a,vector<int> &b);
void main()
{
int str1[]={1,1,1,1,0,0},str2[]={1};
vector<int> a(str1,str1+sizeof(str1)/sizeof(str1[0])),aa(str2,str2+sizeof(str2)/sizeof(str2[0]));
vector<int> b(add(a,aa));
for(vector<int>::iterator it=b.begin();it!=b.end();it++)
cout<<*it<<' ';
cout<<endl;
}
vector<int> add(vector<int> &a,vector<int> &aa)
{
int top1=-1,top2=-1;
int stack1[10],stack2[10];
vector<int> result;
for(int i=a.size()-1;i>=0;i--) stack1[++top1]=a[i];
for(i=aa.size()-1;i>=0;i--) stack2[++top2]=aa[i];//放到栈中
int last1=0,last2=0;
while(last1<=top1 && last2<=top2)
{
if(stack1[last1]+stack2[last2]==2)//进位
{
int tmp=last1;
stack1[tmp++]=0;
while(stack1[tmp]==1 && tmp<=top1) { stack1[tmp]=0;tmp++;}
if(tmp>top1) stack1[++top1]=1;
else stack1[tmp]=1;
}
else //不进位
{
stack1[last1]=stack1[last1]+stack2[last2];
}
last1++;last2++;
}
if(last2<=top2) stack1[top1++]=stack2[last2++];//stack1为最终计算结果
while(top1>=0) result.push_back(stack1[top1--]);//出栈
return result;
}
#include<vector>
#include<string>
using namespace std;
vector<int> add(vector<int> &a,vector<int> &b);
void main()
{
int str1[]={1,1,1,1,0,0},str2[]={1};
vector<int> a(str1,str1+sizeof(str1)/sizeof(str1[0])),aa(str2,str2+sizeof(str2)/sizeof(str2[0]));
vector<int> b(add(a,aa));
for(vector<int>::iterator it=b.begin();it!=b.end();it++)
cout<<*it<<' ';
cout<<endl;
}
vector<int> add(vector<int> &a,vector<int> &aa)
{
int top1=-1,top2=-1;
int stack1[10],stack2[10];
vector<int> result;
for(int i=a.size()-1;i>=0;i--) stack1[++top1]=a[i];
for(i=aa.size()-1;i>=0;i--) stack2[++top2]=aa[i];//放到栈中
int last1=0,last2=0;
while(last1<=top1 && last2<=top2)
{
if(stack1[last1]+stack2[last2]==2)//进位
{
int tmp=last1;
stack1[tmp++]=0;
while(stack1[tmp]==1 && tmp<=top1) { stack1[tmp]=0;tmp++;}
if(tmp>top1) stack1[++top1]=1;
else stack1[tmp]=1;
}
else //不进位
{
stack1[last1]=stack1[last1]+stack2[last2];
}
last1++;last2++;
}
if(last2<=top2) stack1[top1++]=stack2[last2++];//stack1为最终计算结果
while(top1>=0) result.push_back(stack1[top1--]);//出栈
return result;
}
如何评价这个??
我想知道标准。