def find_average(arr): n = len(arr) cnt = 0 i, j = 0, n - 1 while i < j: mid = (arr[i] + arr[j]) / 2 if mid in arr and mid != arr[i] and mid != arr[j]: cnt += 1 if mid < arr[0]: i += 1 elif mid > arr[-1]: j -= 1 else: if mid > arr[i+1]: i += 1 elif mid < arr[j-1]: j -= 1 else: i += 1 j -= 1 return cnt 把这段代码改为c语言
时间: 2024-01-24 14:05:05 浏览: 83
以下是将该Python代码转换为C语言的代码:
```
#include <stdio.h>
int find_average(int arr[], int n) {
int cnt = 0;
int i = 0, j = n - 1;
while (i < j) {
int mid = (arr[i] + arr[j]) / 2;
int k;
int mid_in_arr = 0;
for (k = 0; k < n; k++) {
if (arr[k] == mid && mid != arr[i] && mid != arr[j]) {
mid_in_arr = 1;
break;
}
}
if (mid_in_arr) {
cnt++;
}
if (mid < arr[0]) {
i++;
} else if (mid > arr[n-1]) {
j--;
} else {
if (mid > arr[i+1]) {
i++;
} else if (mid < arr[j-1]) {
j--;
} else {
i++;
j--;
}
}
}
return cnt;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int cnt = find_average(arr, n);
printf("%d\n", cnt);
return 0;
}
```
阅读全文