如题所述
#include <stdio.h>
#include<stdlib.h>
int searchPos(int arr[], int len, int e) {
for (int i = 0; i < len; i++) {
if (arr[i] == e)
return i + 1;
}
return -1;
}
int main() {
int len, e, pos;
printf("请输入数组长度:\n");
scanf("%d", &len);
int* arr = (int*)malloc(sizeof(int) * len);
printf("请输入%d个数组元素:\n",len);
for (int i = 0; i < len; i++) {
scanf("%d", &arr[i]);
}
printf("请输入要查找的数:\n");
scanf("%d", &e);
pos = searchPos(arr, len, e);
printf("要查找的数在第%d位。\n", pos);
return 0;
}
#include<stdlib.h>
int searchPos(int arr[], int len, int e) {
for (int i = 0; i < len; i++) {
if (arr[i] == e)
return i + 1;
}
return -1;
}
int main() {
int len, e, pos;
printf("请输入数组长度:\n");
scanf("%d", &len);
int* arr = (int*)malloc(sizeof(int) * len);
printf("请输入%d个数组元素:\n",len);
for (int i = 0; i < len; i++) {
scanf("%d", &arr[i]);
}
printf("请输入要查找的数:\n");
scanf("%d", &e);
pos = searchPos(arr, len, e);
printf("要查找的数在第%d位。\n", pos);
return 0;
}
温馨提示:答案为网友推荐,仅供参考