本文发表在 rolia.net 枫下论坛#include <stdio.h>
int print_array(int *input, int number)
{
int i;
printf("number = %d [", number);
for(i=0; i<number; i++)
{
printf("%d ", *input);
input++;
}
printf("]\n");
}
//return:
// -1 - empty array;
// 1 - no even inetegers found in the array;
// even - the multiplied value;
int seek_array(int *input, int number)
{
int i;
int largest_even = 9999;
int the_less_even = 9997;
if (number <= 0 )
return -1;
for(i=0; i<number; i++, input++)
{
if (((*input)/2)*2 != *input) continue;
if (largest_even == 9999 && the_less_even == 9997)
largest_even = *input;
else if (the_less_even == 9997)
{
if (*input <= largest_even)
the_less_even = *input;
else
{
the_less_even = largest_even;
largest_even = *input;
}
}
else
{
if (*input < the_less_even)
;
else if (*input == the_less_even)
;
else if (*input > largest_even)
{
the_less_even = largest_even;
largest_even = *input;
}
else if (*input == largest_even)
the_less_even = largest_even;
else //in case that *input less than the_less_even;
the_less_even = *input;
}
}
printf("largest_even = %d, the_less_even=%d\n", largest_even, the_less_even);
if (largest_even == 9999 && the_less_even == 9997)
return 1;
else if (the_less_even == 9997)
return largest_even;
else
return largest_even*the_less_even;
}
int main()
{
// int int_array[]={};
// int int_array[]={3};
// int int_array[]={4};
// int int_array[]={0, 0};
// int int_array[]={12, 10};
// int int_array[]={0, 0, 2};
// int int_array[]={135, 51, 92147};
// int int_array[]={1, 2, 3, 5, -4, 9, -8};
int int_array[]={0, 1, 0, 2, 3, 4, 5, 10, 5, 20, -2, 9};
int return_value=0;
print_array(&int_array[0], sizeof(int_array)/sizeof(int));
return_value = seek_array(&int_array[0], sizeof(int_array)/sizeof(int));
printf("seek_array() returned: %d\n", return_value);
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
int print_array(int *input, int number)
{
int i;
printf("number = %d [", number);
for(i=0; i<number; i++)
{
printf("%d ", *input);
input++;
}
printf("]\n");
}
//return:
// -1 - empty array;
// 1 - no even inetegers found in the array;
// even - the multiplied value;
int seek_array(int *input, int number)
{
int i;
int largest_even = 9999;
int the_less_even = 9997;
if (number <= 0 )
return -1;
for(i=0; i<number; i++, input++)
{
if (((*input)/2)*2 != *input) continue;
if (largest_even == 9999 && the_less_even == 9997)
largest_even = *input;
else if (the_less_even == 9997)
{
if (*input <= largest_even)
the_less_even = *input;
else
{
the_less_even = largest_even;
largest_even = *input;
}
}
else
{
if (*input < the_less_even)
;
else if (*input == the_less_even)
;
else if (*input > largest_even)
{
the_less_even = largest_even;
largest_even = *input;
}
else if (*input == largest_even)
the_less_even = largest_even;
else //in case that *input less than the_less_even;
the_less_even = *input;
}
}
printf("largest_even = %d, the_less_even=%d\n", largest_even, the_less_even);
if (largest_even == 9999 && the_less_even == 9997)
return 1;
else if (the_less_even == 9997)
return largest_even;
else
return largest_even*the_less_even;
}
int main()
{
// int int_array[]={};
// int int_array[]={3};
// int int_array[]={4};
// int int_array[]={0, 0};
// int int_array[]={12, 10};
// int int_array[]={0, 0, 2};
// int int_array[]={135, 51, 92147};
// int int_array[]={1, 2, 3, 5, -4, 9, -8};
int int_array[]={0, 1, 0, 2, 3, 4, 5, 10, 5, 20, -2, 9};
int return_value=0;
print_array(&int_array[0], sizeof(int_array)/sizeof(int));
return_value = seek_array(&int_array[0], sizeof(int_array)/sizeof(int));
printf("seek_array() returned: %d\n", return_value);
}更多精彩文章及讨论,请光临枫下论坛 rolia.net