[Function Question 8] PTA #C

5 篇文章 0 订阅

Question

1. We will write a function Count the number of occurrences of a particular number in a string of numbers
2. which looks like: int CountDigit( int number, int digit );
3. The number we input should be contained by the “long” type.
4. The ‘digit’ should be an integer number in range of [0,9]

Test Samples

#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;
}


we will add our functions at the end of the code

Input Samples

-21252 2


Output Samples

Number of digit 2 in -21252: 3


Note Source of the question: 浙大版《C语言程序设计（第3版）》题目集

Ideas to solve the question

We can see from the Output Samples that we will only put the number we count at the end of the function.
To solve this question, we can iterate the whole number and compare every number with the ‘digit’.
There are two method to reach that, one way is get the remainder int the loop to get every number, another idea is that we can change the type of the number, we will change it into a char type instead of the integer, and in the loop we compare the ASCII code of the number and the digit
But we will use the itoa function in the <stdlib.h> library, we can see the head of the code, we do not include this library, so method seems can not use in this question.

int CountDigit( int number, int digit)
{
int result = 0;
int element;

while (number != 0) {
element = number % 10;
if (digit == element) {
result += 1;
}
number = number / 10;
}
return result;
}


we define the element variable to get every piece of number in tnumber, and compare it with the digit variable.
You might also notice that I use a while loop but not a for loop, because we do not know the length of the number, and we will reduce the loop by divide the numbver by 10 repeatly until it become to 0.

we can run it…

Number of digit 2 in -21252: 3


And we get it!

So the whole code looke like that:

#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)
{
int result = 0;
int element;
if (number < 0) {
number = - number;
}
while (number != 0) {
element = number % 10;
if (digit == element) {
result += 1;
}
number = number / 10;
}
return result;
}


Some ways to make it better…

As we mentioned the second way (change it into char type and compare it)
I slao get the code but there are some small bugs about change the digit variablre into char type.

#include <stdio.h>
#include <stdlib.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)
{
int result = 0;
char string[16] = {0};
char Cdigit[16] = {0};
itoa(number,string,10);
itoa(digit,Cdigit,10);
for (int i=0;i<16;i++) {
if (string[i] == Cdigit[0]) {
result += 1;
}
}

}



When I run it, it will looks like:
input:

-21252 2


output:

Number of digit 2 in -21252: 50


How could it be 50???

• 0
点赞
• 0
收藏 更改收藏夹
• 打赏
• 0
评论
06-21 1772
09-23

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

Naux334

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。