习题5-5 使用函数统计指定数字的个数 (15分)

本题要求实现一个统计整数中指定数字的个数的简单函数。

函数接口定义:

int CountDigit( int number, int digit );

其中number是不超过长整型的整数,digit为[0, 9]区间内的整数。函数CountDigit应返回numberdigit出现的次数。

裁判测试程序样例:

#include <stdio.h>

int CountDigit( int number, int digit );

int main()
{
    int number, digit;

    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

-21252 2

输出样例:

Number of digit 2 in -21252: 3
int CountDigit( int number, int digit )
{
 int a;
 int count=0;
 if(number<0){
         number*=-1;
    }
 while(number>=10){
   a=number%10;
   number=number/10;
       if(a==digit){
        count=count+1;
       }
 }
 if(number==digit)
  count=count+1;

 return count; 

}

原文地址:https://www.cnblogs.com/Kimsohyun/p/12578916.html

时间: 2024-08-30 12:12:53

习题5-5 使用函数统计指定数字的个数 (15分)的相关文章

统计指定数字的个数,并将其按权值10的次方数相加

题目描述: 正整数A的"DA(为一位整数)部分"定义由A中所有DA组成新的整数PA.例如,给定A=3862767,DA=6,则A的"6部分"PA是66,因为A中有2个6 现给定:A.DA.B.DB,请编写程序计算PA+PB 输入描述: 输入在一行中依次给出A.DA.B.DB,中间以空格分隔,其中0<A,B<1010 输出描述: 在一行中输出PA+PB的值 如图,就是查询第一个整数中6的个数,2,再变成66:查询第二个整数3的个数,3,再变成333 再相加

浙大版《C语言程序设计(第3版)》题目集 习题2-6 求阶乘序列前N项和 (15 分)

习题2-6 求阶乘序列前N项和 (15 分) 本题要求编写程序,计算序列 1!+2!+3!+? 的前N项之和. 输入格式: 输入在一行中给出一个不超过12的正整数N. 输出格式: 在一行中输出整数结果. 输入样例: 5 输出样例: 153 思路:利用函数阶乘累加. 代码如下: #include <stdio.h> long factorial(long n); int main() { long n; scanf("%ld",&n); printf("%l

统计指定数字和

输入一个数字的字符串和一个数字字符(1到9),统计这个数字在字符串中出现的次数,并累加.例如有个字符串是1234321,查找2这个数字,在字符串中出现了2次,所以累计和就是2+2=4 输入格式: 输入2行.第1行是字符串,第2行是要查找的数字. 输出格式: 指定数字出现的累加和 输入样例: 1234321 2 输出样例: 4 n=str(input()) p=str(input()) print(n.count(p)*int(p)) 原文地址:https://www.cnblogs.com/Sk

武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数

Problem Description 输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符) Input 多组测试数据,每行一组 Output 每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数 Sample Input I am a student in class 1. I think I can! Sample Output 18 1 6 1 10 0 3 1 HINT char str[100];//定义字符型数组 while(g

习题6-8 统计一行文本的单词个数 (15分)

本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入给出一行字符. 输出格式: 在一行中输出单词个数. 输入样例: Let's go to room 209. 输出样例: 5 发现别人都是用数组,但是我自己觉得这个方法比较容易理解. #include<stdio.h> int main(void) { char ch='0'; int sum=0; int sign; while(ch!='\n'){ si

练习5-3 数字金字塔(15 分)

输入样例: 5 输出样例: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 #include<stdio.h> int sum(int n) { for(int i=1;i<=n;i++)//n行 { for(int j=0;j<n-i;j++)//左边空格数 { printf(" "); } for(int j=1;j<=i;j++)//输出数字,数字后有一个空格 { printf("%d ",i); } printf(&

习题2.5 两个有序链表序列的合并 (15分)

本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列. 函数接口定义: List Merge( List L1, List L2 ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; typedef PtrToNode List; /* 定义单链表类型 */ L1和L2

第3章-14.统计一行文本的单词个数 (15分)

本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入给出一行字符. 输出格式: 在一行中输出单词个数. 输入样例: Let's go to room 209. 输出样例: 5 1 # 统计一行文本的单词个数 2 # Author: cnRick 3 # Time : 2020-3-25 4 aStr_list = input().split() 5 result = len(aStr_list) 6 prin

PTA 统计二叉树叶子结点个数

6-2 统计二叉树叶子结点个数 (10 分) 本题要求实现一个函数,可统计二叉树的叶子结点个数. 函数接口定义: int LeafCount ( BiTree T); T是二叉树树根指针,函数LeafCount返回二叉树中叶子结点个数,若树为空,则返回0. 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> typedef char ElemType; typedef struct BiTNode { ElemType data; st