기본 콘텐츠로 건너뛰기

2577 : 숫자의 개수 (Implementation) [C++]

먼저 문제를 푼 방식을 설명하겠다.

예를 들어 12345를 10으로 나눈 나머지는 5가 나온다.

다시 1234를 10으로 나눈 나머지는 4가 나온다.

다시 123을 10으로 나눈 나머지는 3이 나온다.

다시 12를 10으로 나눈 나머지는 2가 나온다.

마지막으로 1을 10으로 나눈 나머지는 1이 나온다.

이렇게 세 개의 숫자를 모두 곱한 숫자를 10으로 나머지 연산을 하고

나온 숫자를 0~9까지의 개수를 기록하는 배열에 저장하고

숫자를 다시 10으로 나눠주는 연산을 반복하면 된다.

그리고 배열의 값들을 출력해주면 된다.

void Number::GetValue()
{
 for (int i = 10; this->Sum > 0; this->Sum /= 10)
 {
  int Num = this->Sum % i;
  this->Array[Num]++;
 }

 for (int i = 0; i < 10; i++)
  cout << this->Array[i] << endl;
}
<소스 코드>

*Source of the problem = https://www.acmicpc.net/problem/2577
*문제 출처 : 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