回归 pta 今天在上机实验时,回归数据结构PTA
有序表合并
#include <iostream>
using namespace std;
#define Maxsize 100
struct List{
int data[Maxsize];
int length;
};
List meger(List A,List B ){
List c;
int i=0,j=0,k=0;
while(i<A.length&&j<B.length){
if(A.data[A.length-1-i]<=B.data[B.length-1-j]){
c.data[k]=A.data[A.length-1-i];
i++;
k++;
}
else{
c.data[k]=B.data[B.length-1-j];
j++;
k++;
}
}
while(i<A.length){
c.data[k]=A.data[A.length-1-i];
i++;
k++;
}
while(j<B.length){
c.data[k]=B.data[B.length-1-j];
j++;
k++;
}
c.length=k;
return c;
}
void removeDuplicates(List &C) {
for (int i = 0; i < C.length - 1; i++) {
if (C.data[i] == C.data[i + 1]) {
for (int j = i; j < C.length - 1; j++) {
C.data[j] = C.data[j + 1];
}
C.length--;
i--;
}
}
}
int main(){
List A,B;
int n=0;
for(int i=0;;i++){
cin>>n;
if(n!=-1){
A.data[i]=n;
A.length++;
}
else{
break;
}
}
for(int i=0;;i++){
cin>>n;
if(n!=-1){
B.data[i]=n;
B.length++;
}
else{
break;
}
}
List c=meger(A,B);
removeDuplicates(c);
for(int i=0;i<c.length;i++){
if(i!=c.length-1){
cout<<c.data[i]<<",";
}
else{
cout<<c.data[i];
}
}
return 0;
}