UIU Competitive Programmers contest – 02 Solution


A – QQ


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main ()
{
    for (int firstNumber = 1; firstNumber <= 9; firstNumber++) {
        for (int secondNumber = 1; secondNumber <= 9; secondNumber++) {
            printf ("%dx%d=%d\n", firstNumber, secondNumber, firstNumber * secondNumber);
        }
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – Sorting Five Numbers


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <algorithm>
#import <functional>

using namespace std;

int main ()
{
    int nums [5];

    for (int i = 0; i < 5; i++)
        scanf ("%d", &nums [i]);

    // descending order sort
    sort(nums, nums + 5, greater<int>());

    bool space = false;

    for (int i = 0; i < 5; i++) {
        if (space) printf (" ");
        space = true;
        printf ("%d", nums [i]);
    }

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

C – Digit Number


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main ()
{
    int a, b;

    while (scanf ("%d %d", &a, &b) != EOF) {
        int sum = a + b;
        int ret = 0;

        while(sum) {
            sum /= 10;
            ret++;
        }

        printf("%d\n", ret);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

D – GCD and LCM


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

#define LL long long

using namespace std;

LL gcd(LL a, LL b)
{
    if (b == 0) return a;

    return gcd(b, a % b);
}

int main ()
{
    LL a, b;

    while(cin >> a >> b) {
        LL gcdVal = gcd(a, b);
        LL lcmVal = a * b / gcdVal;

        cout << gcdVal << " " << lcmVal << endl;
    }


    return 0;
}

// @END_OF_SOURCE_CODE

E – List of Top 3 Hills


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <algorithm>

using namespace std;

int main ()
{
    int heights [10 + 5];

    for (int i = 0; i < 10; i++) {
        scanf ("%d", &heights[i]);
    }

    sort(heights, heights + 10);

    printf ("%d\n%d\n%d\n", heights [9], heights [8], heights [7]);

    return 0;
}

// @END_OF_SOURCE_CODE

Advertisements

One thought on “UIU Competitive Programmers contest – 02 Solution

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s