其实我是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");
}
答案 0 :(得分:0)
#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");
}