기본 콘텐츠로 건너뛰기

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

댓글

이 블로그의 인기 게시물

1978 : 소수 찾기 [C++]

# include < iostream > # include < vector > using namespace std ; int main ( ) { cin . tie ( NULL ) ; vector < int > Primes ; Primes . push_back ( 2 ) ; Primes . push_back ( 3 ) ; for ( int i = 4 ; i < 1000 ; i + + ) { bool IsPrime = true ; if ( i % 2 = = 0 | | i % 3 = = 0 ) continue ; for ( int j = 4 ; j < i ; j + + ) { if ( i % j = = 0 ) { IsPrime = false ; break ; } } if ( IsPrime ) Primes . push_back ( i ) ; } int N , Count = 0 ; cin > > N ; for ( int i = 0 ; i < N ; i + + ) { int Input ; cin > > Input ; for ( int j = 0 ; j < Primes . size ( ) ; j + + ) if ( Input = = Primes [ j ] ) Count + + ; } cout < < Count < < " \n " ; return 0 ; }

10828 : 스택 [Python]

Stack = [ ] def push ( num ) : Stack . append ( int ( num ) ) def pop ( ) : if len ( Stack ) > 0 : print ( Stack . pop ( ) ) else : print ( - 1 ) def size ( ) : print ( len ( Stack ) ) def empty ( ) : if len ( Stack ) == 0 : print ( 1 ) else : print ( 0 ) def top ( ) : if len ( Stack ) > 0 : print ( Stack [ len ( Stack ) - 1 ] ) else : print ( - 1 ) TestCase = int ( input ( ) ) while TestCase > 0 : Command = input ( ) if Command == 'top' : top ( ) elif Command == 'pop' : pop ( ) elif Command == 'empty' : empty ( ) elif Command == 'size' : size ( ) else : push ( Command [ 5 : ] ) TestCase - = 1