输入 n 个 1∼2000 范围内的整数。
将其从小到大进行插入排序,第 i 步将 a[i] 插入到合适的位置,输出每个元素插入后的状态。
第一行一个整数 n。 接下来一行 n 个整数,含义如题意所述。
5
7 6 3 8 2
n 行每行 n 个空格隔开的整数,含义如题意所述。
7 6 3 8 2
6 7 3 8 2
3 6 7 8 2
3 6 7 8 2
2 3 6 7 8
对于 100% 的数据,1≤n≤100。
#include <bits/stdc++.h>
using namespace std;
int a[110];
int main()
{
int n;
cin >> n;
for(int i = 0;i < n;i++)
cin >> a[i];
for(int i = 1;i < n;i++)
{
if(a[i] < a[i - 1])
{
int temp = a[i];// 6
int j = i - 1;
for(;j >= 0 &&temp <a[j];j--)
{
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
for(int i = 0;i < n;i++)
{
cout << a[i] << ' ';
}
cout << endl;
}
return 0;
}
Wrong Answer 0 Standard answer longer than user output.
这是为什么呢?