#include<iostream>
using namespace std;
struct tree{
//string date;
int weight, parent, lchild, rchild;
};
void creat(struct tree *t, int n);
//void trave(struct tree *t);
int main(){
int n, choice;
n = 8;
int m = 2*n;
tree *t = new tree[m];
//cout<<"输入叶子个数"<<endl;
//cin<<n;
creat(t, n);
//cout<<t[m-1].weight<<endl;
/*while(1){
cout<<"请选择"<<endl;
cin>>choice;
switch(choice){
case 1:
creat(t, n);
break;
case 2:
cout<<t[2*n-1].weight<<endl;
break;
}
}*/
}
void creat(struct tree *t, int n){
int i, o, p;
int m = 2*n;
for(i=1;i<=m;i++){
t[i].lchild = 0;
t[i].rchild = 0;
t[i].parent = 0;
}//把这个for循环删掉之后就正常了
cout<<"输入权重"<<endl;
for(i=1;i<=n;i++){
//cin.clear();
cin>>p;//错误处
cout<<p<<endl;
t[i].weight = p;
}
int m1= 1, m2= 1;
for(o=n+1;o<=m-1;o++){
for(i=1;i<o;i++){
if(t[i].weight < t[m1].weight && t[i].parent == 0){
m1 = i;
}
}
for(i=1;i<=o-1;i++){
if(t[i].weight < t[m1].weight && i != m1 && t[i].parent == 0){
m2 = i;
}
}
t[o].weight = t[m1].weight + t[m2].weight;
t[o].lchild = m1;
t[o].rchild = m2;
t[o].parent = 0;
t[m1].parent = o;
t[m2].parent = o;
m1 = o;
m2 = o;
}
}
当用cin输入后再输出p,输出的不是之前输入的数,并且会跳过之后的cin