UIU Competitive Programmers Contest – 01 Solution


A – A+B for Input-Output Practice (I)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (scanf ("%d %d", &a, &b) != EOF) {
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – A+B for Input-Output Practice (II)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    scanf ("%d", &n);

    int a;
    int b;

    while (n--) {
        scanf ("%d %d", &a, &b);
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

C – A+B for Input-Output Practice (III)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (true) {
        scanf ("%d %d", &a, &b);
        if (a == 0 && b == 0) break;
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

D – A+B for Input-Output Practice (IV)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    while (true) {
        scanf ("%d", &n);

        if (n == 0) break;
        
        int num;
        int sum = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }
        
        printf ("%d\n", sum);
    }



    return 0;
}

// @END_OF_SOURCE_CODE

E – A+B for Input-Output Practice (V)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    scanf ("%d", &n);

    while (n--) {
        
        int m;
        scanf ("%d", &m);
        
        int num;
        int sum = 0;

        for ( int i = 0; i < m; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }

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

    return 0;
}

// @END_OF_SOURCE_CODE

F – A+B for Input-Output Practice (VI)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    while (scanf ("%d", &n) != EOF) {

        int num;
        int sum = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }

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

    return 0;
}

// @END_OF_SOURCE_CODE

G – A+B for Input-Output Practice (VII)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (scanf ("%d %d", &a, &b) != EOF) {

        printf ("%d\n\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

H – A+B for Input-Output Practice (VIII)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;
    
    scanf ("%d", &n);
    
    bool blankLine = false;
    
    while (n--) {
        
        int m; 
        scanf ("%d", &m);
        
        int num;
        int sum = 0;
        
        for ( int i = 0; i < m; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }
        
        if (blankLine) printf ("\n");
        
        blankLine = true;
        
        printf ("%d\n", sum);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

I – Reverse Sequence


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

int main ()
{
    char str [20 + 10];

    scanf ("%s", str);

    int length = strlen(str);

    for ( int i = length - 1; i >= 0; i-- )
        printf ("%c", str [i]);

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

J – Factorial


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main ()
{
    int n;

    scanf("%d", &n);

    long long fact = 1;

    for ( int i = 1; i <= n; i++ )
        fact *= i;

    cout << fact << endl;

    return 0;
}

// @END_OF_SOURCE_CODE

K – Prime Number


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

bool prime [999999 + 10];

bool isPrime(int n) {

    // all numbers less than 2 is not prime
    if (n < 2) return false;

    // 2 is only even prime
    if (n == 2) return true;

    // others even numbers are not prime
    if (n % 2 == 0) return false;

    int squareRoot = (int) sqrt(n);

    for ( int i = 3; i <= squareRoot; i += 2 ) {
        if (n % i == 0) return false;
    }

    return true;
}

void preCalculation()
{
    for ( int i = 0; i <= 999999; i++ ) {
        prime [i] = isPrime(i);
    }

}

int main ()
{
    preCalculation();

    int n;

    while (scanf ("%d", &n) != EOF) {

        int count = 0;

        for ( int i = 2; i <= n; i++ ) {
            if (prime [i]) count++;
        }

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

    return 0;
}

// @END_OF_SOURCE_CODE

Advertisements

One thought on “UIU Competitive Programmers Contest – 01 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