織夢(mèng)網(wǎng)站案例企業(yè)網(wǎng)站推廣建議
合抱之木,生于毫末;九層之臺(tái),起于累土;千里之行,始于足下。💪🏻
一、題目描述 ??
二、代碼(C語(yǔ)言)??
#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;
}/* 你的代碼將被嵌在這里 */
int CountDigit(int number, int digit) {// 特殊情況:如果 number 是 0 且 digit 也是 0,直接返回 1if (number == 0 && digit == 0) {return 1;}int count = 0; // 初始化計(jì)數(shù)器,用于記錄 digit 在 number 中出現(xiàn)的次數(shù)// 如果 number 是負(fù)數(shù),將其轉(zhuǎn)換為正數(shù)if (number < 0) {number *= (-1);}// 遍歷 number 的每一位while (number != 0) {// 取出 number 的最后一位int lastDigit = number % 10;// 如果最后一位等于 digit,計(jì)數(shù)器加 1if (lastDigit == digit) {count++;}// 去掉 number 的最后一位number /= 10;}// 返回 digit 在 number 中出現(xiàn)的次數(shù)return count;
}