我是靠谱客的博主 美丽黄蜂,这篇文章主要介绍6-2 统计个位数字 (10 分),现在分享给大家,希望可以做个参考。

6-2 统计个位数字 (10 分)
本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。

函数接口定义:
int Count_Digit ( const int N, const int D );
其中N和D都是用户传入的参数。N的值不超过int的范围;D是[0, 9]区间内的个位数。函数须返回N中D出现的次数。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h> int Count_Digit ( const int N, const int D ); int main() { int N, D; scanf("%d %d", &N, &D); printf("%dn", Count_Digit(N, D)); return 0; }

/* 你的代码将被嵌在这里 */
输入样例:

复制代码
1
2
-21252 2

输出样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3 int Count_Digit ( const int N, const int D ) { int count = 0,n=N, temp; if(N<0) n=-N; if(n==D) return 1; do{ temp = n%10; n /= 10; if(temp == D) count++; }while(n); return count; }

千万不要漏了n为个位数的情况

最后

以上就是美丽黄蜂最近收集整理的关于6-2 统计个位数字 (10 分)的全部内容,更多相关6-2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部