기본 콘텐츠로 건너뛰기

1676 : 팩토리얼 0의 개수 [C++]

처음 생각한 방법은 5마다 0이 하나씩 늘어난다는 가정이었다.

4! = 24, 5!  = 120
9! = 362880, 10! = 3628800
14! = 87178291200 15! = 1307674368000

근데 100을 넘어서부터는 하나씩 더 추가되나보다.. 틀렸다고 나왔다.

두번째로 생각한 방법은 뒤의 0만 세는 조건이므로 그 앞의 숫자를 저장해놓고 그 숫자로

만 계산을 하는 방법을 생각했다. 결과는 정답!

1) 마지막 숫자를 Number에 저장해놓고 팩토리얼을 진행한다.

2) 10으로 나눈 나머지가 0이라면(뒤에 0이 있다면) 계속 제거하고 아니라면

3) 마지막 숫자만 남긴 뒤(10으로 나머지 처리한 후) 반복문 탈출. 

long long Factorial(long long num)
{
 int Count = 0;
 int Number = 1;

 for (long long i = 2; i <= num; i++)
 {
  Number *= i;
  while(true)
  {
   if ((Number % 10) == 0)
   {
    Number /= 10;
    Count++;
   }
   else
   {
    Number %= 10;
    break;
   }
  }
 }
 return Count;
}
<소스 코드>

*Source of the problem = https://www.acmicpc.net/problem/1676
*문제 출처 : BAEKJOON ONLINE JUDGE

댓글

이 블로그의 인기 게시물

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

11004 : K번째 수 [C++]

# include < iostream > # include < cstdio > # include < algorithm > int main ( ) { int * Number = new int [ 5000000 ] ; int N , K ; scanf ( " %d %d " , & N , & K ) ; for ( int i = 0 ; i < N ; i + + ) scanf ( " %d " , Number [ i ] ) ; std :: sort ( Number , Number + N ) ; printf ( " %d " , Number [ K - 1 ] ) ; return 0 ; }

1149 : RGB Street Coloring (Dynamic Programming) [C,C++]

The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house.  You must use DP because you must use the previous value.  Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time.  If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house.  Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem =  https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE