Attention: 如果喜歡我寫的文章,歡迎來我的github主頁給star
Github:github.com/MuziJin
本題要求實(shí)現(xiàn)一個(gè)函數(shù),可統(tǒng)計(jì)任一整數(shù)中某個(gè)位數(shù)出現(xiàn)的次數(shù)。例如-21252中,2出現(xiàn)了3次,則該函數(shù)應(yīng)該返回3。
函數(shù)接口定義:
int Count_Digit ( const int N, const int D );
其中N和D都是用戶傳入的參數(shù)。N的值不超過int的范圍;D是[0, 9]區(qū)間內(nèi)的個(gè)位數(shù)。函數(shù)須返回N中D出現(xiàn)的次數(shù)。
裁判測試程序樣例:
#include <stdio.h>
int Count_Digit ( const int N, const int D );
int main()
{
int N, D;
scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}
/* 你的代碼將被嵌在這里 */
輸入樣例:
-21252 2
輸出樣例:
3
Code
int Count_Digit ( const int N, const int D )
{
int temp;
int n = abs(N);
int d = 0;
if(n == 0 && N == 0) //注意0的情況
d = 1;
while( n>0 )
{
temp = n%10;
if( temp == D)
d++;
n /= 10;
}
return d;
}
轉(zhuǎn)載請(qǐng)注明出處:github.com/MuziJin