程序代码:
#include <stdio.h>
#define N 10
void Convert_NegVal_to_posVal(int* a, int length)
{
for(size_t i = 0; i != length; ++i){
if(a[i] < 0){
a[i] = -a[i];
}
}
}
unsigned odd(int* a, int length)
{
unsigned odd = 0;
for(size_t i = 0; i < length; ++i){
if(a[i]%2){
++odd;
}
}
return odd;
}
unsigned even(int* a, int length)
{
unsigned even = 0;
for(size_t i = 0; i < length; ++i){
if(!(a[i]%2)){
++even;
}
}
return even;
}
unsigned prime(int* a, int length)
{
unsigned prime = 0;
int i, j;
for(i = 0; i < length; ++i){
for( j = 2; j <= a[i]; ++j){
if(0 == a[i] % j){
break;
}
}
if(j == a[i]){
++prime;
}
}
return prime;
}
unsigned complete(int* a, int length)
{
unsigned complete = 0;
unsigned sum;
size_t i, j;
for(i = 0; i < length; ++i){
for( j = 1, sum = 0; j < a[i]; ++j){
if(0 == a[i]%j){
sum += j;
}
}
if(0 == a[i]){
continue;
}
if(sum == a[i]){
++complete;
}
}
return complete;
}
int main(void)
{
int a[N] = {0};
unsigned oddval, evenval, primeval, completeval;
oddval = evenval = primeval = completeval = 0;
printf("Please you input the array element: ");
for(size_t i = 0; i != N; ++i){
scanf("%d", &a[i]);
}
int length = sizeof(a)/sizeof(int);
//将负数转换成正数
Convert_NegVal_to_posVal(a, length);
//判断奇数、偶数、素数和 完数的个数
oddval = odd(a, length);
evenval = even(a, length);
primeval = prime(a, length);
completeval = complete(a, length);
printf("odd has %d, even has %d, prime has %d, complete has %d\n", oddval, evenval, primeval, completeval);
return 0;
}