gpt4 book ai didi

c - 如何在 AP 中查找缺失的术语

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:32 25 4
gpt4 key购买 nike

其实我是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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com