Friday, May 27, 2016

URI Online Judge Solution || 1028 Collectable Cards


URI Online Judge | 1028

Collectable Cards

By Neilor Tonin, URI  Brazil
Timelimit: 1
Richard and Vicent are crazy about football collectable cards. In their spare time, they arrange a way of playing some game involving such cards. Both also have the habit of exchanging the repeated cards with their friends, and one day they thought about a different game. They called all their friends and proposed the following: with the cards in hand, each one tried to make an exchange with his closest friend following this simple rule: each one must count how many cards he owned. After this, they had to split these cards into stacks, all of it with the same size, as large as it was possible for both players. Then, each one choose one of the friend's card stacks to receive. For example, if Richard and Vincent would change the cards with respectively 8 and 12 cards each, both must put his cards in stacks of four cards (Richard would have two stacks and Vincent had three stacks), and both choose a stack from his friend to receive it.

Input

The first input line contains a single integer N (1 ≤ N ≤ 3000), indicating the number of test cases. Each test case contains two integer numbers F1 (1 ≤ F1 ≤ 1000) and F2 (1 ≤ F2 ≤ 1000)  indicating, respectly, the among of collectable cards that Richard and Vicent have to change.

Output

For each test case there will be an integer number at the output, representing the maximum size of the stack of cards which can be exchanged between two players.
Input SampleOutput Sample
3
8 12
9 27
259 111
4
9
37

URI Online Judge Solution || 1028 Collectable Cards in C language


#include <stdio.h>
 
int main()
{
    int i, n, a, b;
    int dividendo, divisor, c;
 
    scanf("%d", &n);
 
    for ( i = 0; i < n; ++i)
    {
        scanf("%d%d", &a, &b);
 
        if(b > a){
            dividendo = b;
            divisor = a;
        }else{
            dividendo = a;
            divisor = b;
        }
 
        while(dividendo % divisor != 0)
        {
            c = dividendo % divisor;
            dividendo = divisor;
            divisor = c;
        }
 
        printf("%d\n", divisor);
    }
 
    return 0;
}

Solution in C++ language:


#include <iostream>
using namespace std;

int main()
{
    int n, a, b;
    int dividendo, divisor, c;

    cin >> n;

    for (int i = 0; i < n; ++i)
    {
        cin >> a >> b;

        if(b > a){
            dividendo = b;
            divisor = a;
        }else{
            dividendo = a;
            divisor = b;
        }

        while(dividendo % divisor != 0)
        {
            c = dividendo % divisor;
            dividendo = divisor;
            divisor = c;
        }

        cout << divisor << endl;
    }

    return 0;
}

See or download code from dropbox
See in URI


No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.