기본 콘텐츠로 건너뛰기

2609 : 최대공약수와 최소공배수 [C++]

int getLCM(int A, int B)
{
 int mulA = A;
 int mulB = B;
 int mulLeast = A * B;
 int ReturnValue;
 while (true)
 {
  if ((mulLeast > mulA) && (mulLeast > mulB))
  {
   if (mulA < mulB)
    mulA += A;
   else if (mulA == mulB)
   {
    ReturnValue = mulA;
    break;
   }
   else
    mulB += B;
  }
  else
  {
   ReturnValue = mulLeast;
   break;
  }
 }

 return ReturnValue;
}

int getGCD(int A, int B)
{
    int ReturnValue;
 
    for(int i = 1; i <= (A < B ? A : B); i++)
    {
        if(A % i == 0 && B % i == 0 )
            ReturnValue = i;    
    }

 return ReturnValue;
}
<소스 코드>

*문제 출처 : 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...