c语言数组题,求解
设计子函数:int GetOdd(int a[],int n) 展开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GetOdd(int a[],int n)
{
int max=-1;
int i;
for(i=0;i<n;i++)
{
if(a[i]%2==1 && a[i]>max)
{
max = a[i];
}
}
return max;
}
int main()
{
int num[10];
int i;
int max;
srand((unsigned int)time(NULL));
for(i=0;i<10;i++)
{
num[i] = rand()%90 + 10;
printf("%d\t",num[i]);
}
printf("\n");
max = GetOdd(num,10);
if(max==-1)
{
printf("NO");
}
else
{
printf("%d\n",max);
}
}
#include <stdlib.h>
#include <time.h>
int Sort(int a[],int n)
{
for (int i = 1; i < n; i++) {
int mid = 0;
int left = 0;
int right = i - 1;
int tmp = a[i];
while(left <= right) {
mid= (left + right) / 2;
if (tmp < a[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
a[j + 1] = a[j];
}
a[left] = tmp;
}
}
int GetOdd(int a[],int n)
{
for (int i = 0; i < n; i++) {
printf("a[%d] = %d\n", i, a[i]);
}
Sort(a, n);
bool found = false;
for (int i = n - 1; i >= 0; i--) {
if (a[i] % 2 != 0) {
return a[i];
}
}
return 0;
}
int main()
{
int a[10] = { 0 };
srand((int)time(0));
for (int i = 0; i < 10; ++i) {
int randomval = rand() % 90 + 10;
a[i] = randomval;
}
int re =GetOdd(a, 10);
if (re != 0) {
printf("found %d\n", re);
}
else {
printf("NO\n");
}
return 0;
}
请采纳,谢谢!