编写函数:有序序列插入数据
#include <stdio.h>
#define MAX_SIZE 1001
int getkey(int key[])
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&key[i]);
}
return n;
}
int getarray(int array[])
{
int i=0;
while(scanf("%d",&array[i])&&array[i])
{
i++;
}
return 0;
}
int insert(int array[], int key,int vol)
{
int i, j, b = 0,c=0,k;
for(i = 0; i <vol; i ++)
if(!array[i]) break;
if(i == vol)
return -1;
for(j = 0; j < i; j ++)
{
if(array[j] == key)
return -2;
else if(array[j] > key)
{
b = 1;
}
}
if(b==1)
{
for(c=0;c<vol;c++)
{
if(array[c]>key)
break;
if(array[c] == 0)
{
array[c] = key;
array[c+1] = 0;
return c;
}
}
for(k=vol;k>c;k--)
array[k]=array[k-1];
array[c]=key;
return c;
}
}
int result(int flag)
{
if(flag==-1)
{
printf("The array if full!\n");
}
else if(flag==-2)
{
printf("The key is already in this array!\n");
}
else
{
printf("The key is inserted in position %d.\n", flag);
}
return 0;
}
int main()
{
int array[MAX_SIZE], cases, ca, i, key[MAX_SIZE], size;
scanf("%d", &cases);
for(ca = 1; ca <= cases; ca++)
{
getarray(array);
size = getkey(key);
printf("Case %d:\n", ca);
for(i = 0; i < size; i++)
result(insert(array, key[i], MAX_SIZE - 1));
}
return 0;
}
为什么我的代码测试结果是对的 但提交到oj上面就显示答案错误50%?
[此贴子已经被作者于2017-12-21 23:42编辑过]