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

题目描述

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

输入格式:

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

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

输入样例:

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

}

相关推荐
2401_841495642 分钟前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦4 分钟前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
独好紫罗兰24 分钟前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_8414956429 分钟前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli29 分钟前
优选算法-字符串
算法
我是咸鱼不闲呀44 分钟前
力扣Hot100系列19(Java)——[动态规划]总结(上)(爬楼梯,杨辉三角,打家劫舍,完全平方数,零钱兑换)
java·leetcode·动态规划
qq7422349841 小时前
APS系统与OR-Tools完全指南:智能排产与优化算法实战解析
人工智能·算法·工业·aps·排程
数智工坊1 小时前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构
A尘埃1 小时前
超市购物篮关联分析与货架优化(Apriori算法)
算法
.小墨迹1 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu