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

题目描述

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

输入格式:

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

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

输入样例:

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

}

相关推荐
ZoeJoy824 分钟前
算法筑基(二):搜索算法——从线性查找到图搜索,精准定位数据
算法·哈希算法·图搜索算法
Alicx.29 分钟前
dfs由易到难
算法·蓝桥杯·宽度优先
_日拱一卒41 分钟前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
云泽8081 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享2 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病2 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和
梯度下降中3 小时前
LoRA原理精讲
人工智能·算法·机器学习
IronMurphy3 小时前
【算法三十一】46. 全排列
算法·leetcode·职场和发展
czlczl200209253 小时前
力扣1911. 最大交替子序列和
算法·leetcode·动态规划
靴子学长4 小时前
Decoder only 架构下 - KV cache 的理解
pytorch·深度学习·算法·大模型·kv