0부터 9999까지 8을 포함하지 않는 수는 총 몇개일까?
8, 108, 888, 9998 등은 8을 포함하고 있는 수입니다. 111, 299, 4 등과 같은 수는 8을 포함하지 않는 수 입니다.
#include <stdio.h>
#include <math.h>
// 숫자를 찾는 함수 입니다.
int count_number(int input_number, int start_n, int end_n)
{
// 8을 찾는 총 count입니다.
int number_find_count = 0;
// start_n + end_n의 개수입니다. 0부터 9999이니 총 10000 입니다.
int total_count = end_n - start_n + 1;
int result_count = 0;
// 찾는 숫자는 1~9로 fix했습니다.
if((input_number < 1 ) || (input_number > 10))
{
printf("Find Number range is from 1 to 9 \r\n");
return -1;
}
// 0~9999의 숫자를 찾습니다.
for(int i = start_n; i< end_n; i++)
{
for(int j = 0; j< 4; j++)
{
// pow라는 함수는 10지수승입니다. 10의 0은 1이고 10의 1승은 10, 2승은 100입니다.
// 10, 100, 1000, 10000으로 나누면서 8이 발견하면 count를 증가하고 loop를 빠져나갑니다.
int mod_value = pow(10, (j+1));
int remainder = input_number * pow(10,j);
if((i%mod_value)==(remainder))
{
number_find_count++;
printf("find value (%d) % (%d) == %(%d) \r\n", i , mod_value, remainder);
break;
}
}
}
printf("A total %d of number(%d) from %d to %d were detected.\r\n", number_find_count ,input_number, start_n, end_n);
// 8을 찾은 결과에서 숫자 전체 개수를 빼면 의도한 결과가 나옵니다.
result_count = total_count - number_find_count;
printf("There are %d numbers excluding %d. \r\n", result_count, input_number);
return 0;
}
int main(int argc, char *argv[])
{
count_number(8, 0, 9999);
return 0;
}
2022年08月20日 21:32
풀이 작성