기본 콘텐츠로 건너뛰기

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



댓글

이 블로그의 인기 게시물

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

1005 : ACM CRAFT (Dynamic Programming) (TopologicalSort) [C,C++]

The key to this problem is to sort through the topological sorting algorithm and solve the problem. The topological sorting algorithm is to list the vertices on the graph in order. ========================================================== public static int[] topologicalSort(boolean[][] adj, int[] indegree, int[] time) {     Queue<Integer> q = new LinkedList<>();     int len = indegree.length;     int[] result = new int[len];     for (int i = 1; i < len; i++) {         if (indegree[i] == 0) {             result[i] = time[i];             q.add(i);             break;         }     }     while (!q.isEmpty()) {         int v = q.poll();         for (int i = 1; i < len; i++) {             if (adj[v...