主题:谁帮我改一改啊,在线等
输入一个按小大顺序输入的数组,再输入一个数字,将它插入到原数组中,且插入数字后的数组仍然的小大顺序;
#include <iostream>
#include <STRING>
using namespace std;
main()
{
int i,j,k,n,b;
bool y;
cout<<"Please input the size of array a: n=";
cin>>n;cout<<endl;
int *a=new int[n];
int *m=new int[n+1];
cout<<"Please input array a in order :"<<endl;
for (i=0;i<n;i++)
cin>>a[i];cout<<endl;
cout<<"Please input a number you want to chick :";
cin>>b;cout<<endl;
for(i=0;i<n;i++)
{
if (b>=a[i]&&b<a[i+1])
{
y=true;
break;
}
else
y=false;
}
if(b>=a[n])
{
m[n]=b;
for (j=0;j<n;j++)
m[j]=a[j];
}
else if(y==true)
{
for (j=0;j<=i;j++)
m[j]=a[j];
m[i+1]=b;
for (j=i+2;j<n+1;j++)
m[j]=a[j-1];
}
cout<<"The ordered array is :"<<endl;
for(k=0;k<n+1;k++)
cout<<m[k]<<'\t';
cout<<endl;
cout<<endl;
return 0;
}
#include <iostream>
#include <STRING>
using namespace std;
main()
{
int i,j,k,n,b;
bool y;
cout<<"Please input the size of array a: n=";
cin>>n;cout<<endl;
int *a=new int[n];
int *m=new int[n+1];
cout<<"Please input array a in order :"<<endl;
for (i=0;i<n;i++)
cin>>a[i];cout<<endl;
cout<<"Please input a number you want to chick :";
cin>>b;cout<<endl;
for(i=0;i<n;i++)
{
if (b>=a[i]&&b<a[i+1])
{
y=true;
break;
}
else
y=false;
}
if(b>=a[n])
{
m[n]=b;
for (j=0;j<n;j++)
m[j]=a[j];
}
else if(y==true)
{
for (j=0;j<=i;j++)
m[j]=a[j];
m[i+1]=b;
for (j=i+2;j<n+1;j++)
m[j]=a[j-1];
}
cout<<"The ordered array is :"<<endl;
for(k=0;k<n+1;k++)
cout<<m[k]<<'\t';
cout<<endl;
cout<<endl;
return 0;
}