其实我是C语言的新手。谁能告诉我我哪里出错了每次我像这样输入
1 5 7 9 11 或 1 3 7 9 11
它给出两个输出
这是代码
main()
{
int i,ino,flag=0;
int ser[2500],dif[2499],fault[3];
printf("Enter number of elements\n");
scanf("%d",&ino);
printf("Enter elements:\n");
for(i=0;i<ino;i++)
{
scanf("%d",&ser[i]);
}
for(i=0;i<ino-1;i++)
{
dif[i]=ser[i+1]-ser[i];
}
for(i=0;i<ino-1;i++)
{
if(dif[i]==dif[i+1])
flag++;
else
{
if(i==0)
{
if(dif[i]==dif[i+2])
printf("Missing numbera is: %d\n",ser[i+1]+dif[i]);
else
printf("Missing numberb is: %d\n",ser[i]+dif[i+1]);
}
else
{
if(dif[i]==dif[i-1])
printf("Missing numberc is: %d\n",ser[i+1]+dif[i]);
else if(dif[i+1]==dif[i-1])
printf("Missing numberd is: %d\n",ser[i]+dif[i+1]);
}
}
}
if(flag==(ino-2))
printf("no term missing");
}
#include <stdio.h>
#include <stdbool.h>
bool has_missing_term(int arr[], int size, int *x){
int i, d = (arr[size-1] - arr[0])/(size-1);
int n;
bool ok = true;
for(n=arr[0], i=1; i < size ; ++i){
n += d;
if(n != arr[i]){
ok = false;
break;
}
}
if(ok)
return false;
d = (arr[size-1] - arr[0])/size;
for(n=arr[0], i=1; i < size ; ++i){
n += d;
if(n != arr[i]){
*x = n;
return true;
}
}
return true;//What happened?
}
int main(){
int i, ino, x;
int ser[2500];
printf("Enter number of elements\n");
scanf("%d",&ino);
printf("Enter elements:\n");
for(i=0;i<ino;i++)
scanf("%d",&ser[i]);
if(has_missing_term(ser, ino, &x))
printf("Missing numbera is: %d\n", x);
else
printf("no term missing");
}
我是一名优秀的程序员,十分优秀!