Problem:
Greatest Common Divisor

Write a program which finds the greatest common divisor of two natural numbers a and b

Input

a and b are given in a line sparated by a single space.

Output

Output the greatest common divisor of a and b.

Constrants

1 ≤ a, b ≤ 109

Hint

You can use the following observation:

For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y)

Sample Input 1

54 20

Sample Output 1

2

Sample Input 2

147 105

Sample Output 2

21

Solution in JAVA (judge verdict: Accepted)



import java.io.*;
public class Main {

    public static void main(String[] args) throws IOException  {

        // ?¨??????\??????????????????????????????
        int c;
        int keta = 0;
        // int hairetsu_keta[] = new int[4]; hairetsu_keta[0] = 0;hairetsu_keta[1] = 0;hairetsu_keta[2] = 0;hairetsu_keta[3] = 0;
        int hairetsu_keta[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // ???????????¨???????????¨????????£????????????
        int j = 0;
        int deru = 0;
        // int hairetsu[] = new int[4]; hairetsu[0] = 0; hairetsu[1] = 0; hairetsu[2] = 0; hairetsu[3] = 0;
        int hairetsu[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//??? ????????¨???????????¨????????£???????????????????????????????????°????¢??????? ?????????20???
        int hairetsu_fugou[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//?¬???????????????¨??????????????¨????????? ???????¬???????????????????????????\???
        int i= 0;
        while ((c = System.in.read()) != -1  )
        {
            // System.out.println(c);
            if ( c > 47 && c < 58) // suuji ga kita
               {
                hairetsu[i] = c-48;
                i++;
                keta++;

                // System.out.println("Qurege_no_kazu_keta_hantaikara:" + Qurage_no_kazu);
               }
            if ( c == 45) // minus ga kita
               {
                hairetsu_fugou[j] = -1;
               }

            if ( c == 10) // return ga kita
            {
                hairetsu_keta[j] = keta;
                deru++; //return ga  kitara while wo deru
                j++;
            }
            if ( c == 32) //space ga kita
            {

                hairetsu_keta[j] = keta;
                keta = 0;//space ga kitara tsugi no moji no tame keta ha sokomade
                j++;
            }
            if (deru > 0)
            {
                break;
                }
        }
           // ?¨??????\?????????????????????????????????????????????

        // ??°??????????????\??????
        //int hairetsu_suuchi[] = new int[4]; hairetsu_suuchi[0] = 0; hairetsu_suuchi[1] = 0; hairetsu_suuchi[2] = 0; hairetsu_suuchi[3] = 0;
        int hairetsu_suuchi[] = {0,0,0,0,0,0,0,0,0,0};//???????????¨????????? ??¨????????£????????????
        int k = 0;
        int m = 0;
        int n = 0;


        while( n < (j + 1))
        {
        while(j > 0 && hairetsu_keta[n] > 0 )
        {
            hairetsu_suuchi[k] = hairetsu_suuchi[k]*10 + hairetsu[m];
            hairetsu_keta[n] = hairetsu_keta[n] - 1;
            m++;
        }
        if (hairetsu_keta[n] == 0)
        {
            if(hairetsu_fugou[n] == -1)
            {
                hairetsu_suuchi[k] = hairetsu_suuchi[k]*(-1);

            }
            n++;
            k++;
        }
        }

        //??°??????????????\???????????????
        /*
          for (int p = 0; p < k - 1 ; p++)

        {
        System.out.println("???????????°??????" + hairetsu_suuchi[p]);
        }

        */
        int x = hairetsu_suuchi[0];
        int y = hairetsu_suuchi[1];
        int z;
        while(x != 0)
        {
        if( y > x )
        {
           z = y;
           y = x;
           x = z;
        }
        x = x%y;
        }
        
        	System.out.println(y);

    }

}