实际上我对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");
}
关于c - 如何在AP中查找缺失词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21349774/