对一个已排序的数组插入一个新的数字排序,
这个代码还是蛮痛快D!分享一下~!
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
////以下适用于从小到大
////插入一个新的数字
////适用于插入的子对象 排序
// int a[6] = {1,3,6,7,9};
// int i,j,x,t;
// cout<<"insert data:";
// cin>>x;
// for(i=0;i<5;i++)
// {
// if(x<a[i])
// break;
// }
// for(j=5;j>i;j--)
// {
// a[j]=a[j-1];
// }
// a[j]=x;
// cout<<"Now array a:\n";
// for(i=0;i<6;i++)
// {
// cout<<a[i];
// cout<<"\n";
// }
//插入一个新的数字 适用于从大到小
//适用于插入的子对象 排序
int a[6] = {9,7,6,3,1};
int i,j,x,t;
cout<<"insert data:";
cin>>x;
for(i=0;i<5;i++)
{
if(x>a[i])
break;
}
for(j=5;j>i;j--)
{
a[j]=a[j-1];
}
a[j]=x;
cout<<"Now array a:\n";
for(i=0;i<6;i++)
{
cout<<a[i];
cout<<"\n";
}
system("pause");
return 0;
}
关于system("pause");
system("pause");是程序内部实现暂停,重新开始需要代码。
关于std::cin>> response;
std::cin>> response;只需要随便按些键就可以继续执行代码。