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

题目描述

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

输入格式:

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

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

输入样例:

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

}

相关推荐
一匹电信狗5 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
鱼跃鹰飞6 小时前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
Queenie_Charlie6 小时前
小陶的疑惑2
数据结构·c++·树状数组
梵刹古音7 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
筵陌7 小时前
算法:模拟
算法
Queenie_Charlie8 小时前
小陶与杠铃片
数据结构·c++·树状数组
We་ct8 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
renhongxia18 小时前
AI算法实战:逻辑回归在风控场景中的应用
人工智能·深度学习·算法·机器学习·信息可视化·语言模型·逻辑回归
CoderCodingNo8 小时前
【GESP】C++四级/五级练习题 luogu-P1223 排队接水
开发语言·c++·算法
民乐团扒谱机8 小时前
【AI笔记】精密光时频传递技术核心内容总结
人工智能·算法·光学频率梳