减治法计算数组中的零个数

题目描述

给定一个整数数组,使用减治法计算数组中的零个数

输入格式:

第一行请输入数组元素个数

第二行请输入数组的元素值

输入样例:

复制代码
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;

}

相关推荐
堕27414 分钟前
java数据结构当中的《排序》(一 )
java·数据结构·排序算法
2302_8138062242 分钟前
【嵌入式修炼:数据结构篇】——数据结构总结
数据结构
Wei&Yan1 小时前
数据结构——顺序表(静/动态代码实现)
数据结构·c++·算法·visual studio code
团子的二进制世界2 小时前
G1垃圾收集器是如何工作的?
java·jvm·算法
吃杠碰小鸡2 小时前
高中数学-数列-导数证明
前端·数学·算法
故事不长丨2 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
long3162 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
近津薪荼2 小时前
dfs专题4——二叉树的深搜(验证二叉搜索树)
c++·学习·算法·深度优先
熊文豪2 小时前
探索CANN ops-nn:高性能哈希算子技术解读
算法·哈希算法·cann
熊猫_豆豆2 小时前
YOLOP车道检测
人工智能·python·算法