test 2.1-1

来源:互联网 发布:仙侣奇缘3 数据库密码 编辑:程序博客网 时间:2024/05/02 04:19

我用了vector构造一个可以改变长度的容器line,在编译时用输入题目给的几个数字即可,以非数字作为输入结束。

#include<iostream>

#include<vector>
using namespace std;
int main()
{
vector<int> line;
cout << "Enter your unsequenced array :" << endl;
int temp;
while (cin>>temp)
{
line.push_back(temp);
}
int n = line.size();
for (int j = 1; j < n; j++)
{
int key = line[j];
int i = j - 1;
while (i>=0 && line[i] > key)  //for test 2.1-2,change this '>' to '<';
{
line[i + 1] = line[i];
i--;
}
line[i+1] = key;
}
cout << "Sequence after programed .\n";
for (int i = 0; i < n; i++)
cout << line[i] << ' ';
return 0;
}
0 0
原创粉丝点击