기본 콘텐츠로 건너뛰기

1850 : 최대공약수 (Euclid) [C++]

유클리드 호제법 사용

#include <iostream>

using namespace std;

long long EuclidGCD(long long A, long long B)
{
    if(A == B)
      return A;
    else if(A == 0 || B == 0)
      return 0;
    else if(A > B)
      return EuclidGCD(B, A-B-1);
    else if(A < B)
      return EuclidGCD(A, B-A-1);
}

int main() 
{
    long long A,B;
    cin >> A >> B;
    
    long long Size = EuclidGCD(A-1, B-1);
 
    for(long long i = 0; i <= Size; i++)
      cout << "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

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 ; }

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...