数据的安插与删除
数据的插入与删除
题目描述:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1014
为什么下面的这段程序总是不通过?
------解决思路----------------------
static int a[10001];
swap在哪里实现?
No elements后面少.
题目描述:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1014
为什么下面的这段程序总是不通过?
#include <iostream>
using namespace std;
int main()
{
int a[10000];
int b;
int i =0;
int x,y;
cin >> b;
//先读取数据
while (b != -1)
{
a[i] = b;
i++;
cin >> b;
}
//读取要添加的数据
cin >> x;
//读取要删除的数据
cin >> y;
if (i>0)
{
//对读取的数据进行排序,最原始的数据用j
for (int j = 0;j<i;j++)
{
for (int k=j+1;k<i;k++)
{
if (a[j] > a[k])
{
swap(a[j],a[k]);
}
}
}
//显示部分
for (int j=0; j<i-1;j++)
{
cout << a[j] << ",";
}
cout << a[i-1];
}
else
cout << "No elements";
cout << endl;
//添加数据后排序,用n
if (i>0)
{
for (int n=0; n<i;n++)
{
if (a[n] > x)
{
for (int m=i-1;m>=n;m--)
{
a[m+1] =a[m];
}
a[n] = x;
break;
}
}
for (int j=0; j<i;j++)
{
cout << a[j] << ",";
}
cout << a[i];
cout << endl;
}
else
{
cout << x << endl;
}
//删除数据后排序,用p,q
int e = i+1;
int p=0;
if (e==1)
{
}
else if (e>1)
{
while ( p < e)
{
if (a[p] == y)
{
for (int q=p ; q<e;q++)
{
a[q] = a[q+1];
}
e = e-1;
continue;
}
p++;
}
}
if (e>1)
{
for (int j=0; j< e-1;j++)
{
cout << a[j] << ",";
}
cout << a[e-1];
}
else
cout << "No elements.";
cout << endl;
return 0;
}
------解决思路----------------------
static int a[10001];
swap在哪里实现?
No elements后面少.