题目描述
给定一个整数数组,使用减治法计算数组中的零个数
输入格式:
第一行请输入数组元素个数
第二行请输入数组的元素值
输入样例:
4
1 2 0 4
输出样例:
1
#include<stdio.h>
int countZero(int arr[], int n) {
if (n==0) {
return 0;
} else {
if (arr[n-1]==0) {
return 1+countZero(arr,n-1);
} else {
return countZero(arr,n-1);
}
}
}
int main(){
//请在此处开始编写你的代码
int n;
printf("Enter the size of the array:");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array:");
for (int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
int count=countZero(arr,n);
printf("The number of zero elements in the array is:%d\n",count);
return 0;
}