UIU Competitive Programmers contest – 05 Solutions


A – Word Reversal


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

char input [1000];

void reverseIt(int start, int end)
{
    for (int i = end - 1; i >= start; i--)
        printf ("%c", input [i]);
}

void printOutput()
{
    int length = strlen(input);
    int start = 0;
    bool space = false;

    for (int i = 0; i <= length; i++ ) {
        if (input [i] == ' ' || input [i] == '\0') {

            if (space) printf (" ");
            space = true;

            reverseIt(start, i);
            start = i + 1;
        }
    }

    printf ("\n");
}

int main(int argc, const char * argv[])
{
    int testCases;
    scanf ("%d", &testCases);

    bool blankLine = false;

    while (testCases--) {

        // There is a blank line between output blocks.
        if (blankLine) printf ("\n");
        blankLine = true;

        int n;

        scanf ("%d", &n);

        // gets will take the \n character after integer input
        // do not use fflush(), always use getchar() 
        getchar();

        while(n--) {
            gets(input);
            printOutput();
        }
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – Easier Done Than Said?


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

char password [20 + 10];
int length;

bool isVowel(char x) {
    return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u';
}


bool isConsonant(char x) {
    return !isVowel(x);
}

bool atLeastOneVowel() {

    for (int i = 0; i < length; i++) {
        if (isVowel(password [i])) return true;
    }

    return false;
}

bool threeConsecutive() {

    for (int i = 2; i < length; i++) {
        if (isVowel(password [i]) && isVowel(password[i - 1]) && isVowel(password [i - 2])) return true;
        if (isConsonant(password [i]) && isConsonant(password[i - 1]) && isConsonant(password [i - 2])) return true;
    }

    return false;
}

bool twoConsecutive() {

    for (int i = 1; i < length; i++) {
        if (password [i] == password [i - 1] && password [i] != 'e' && password [i] != 'o') return true;
    }

    return false;
}

int main(int argc, const char * argv[])
{
    while(scanf ("%s", password)) {

        if (strcmp(password, "end") == 0) break;

        length = strlen(password);

        if (atLeastOneVowel() && !threeConsecutive() && !twoConsecutive()) {
            printf ("<%s> is acceptable.\n", password);

        } else {
            printf ("<%s> is not acceptable.\n", password);
        }
    }

    return 0;
}

// @END_OF_SOURCE_CODE

C – Team


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;


int main(int argc, const char * argv[])
{
    int n;

    scanf ("%d", &n);

    int a;
    int b;
    int c;

    int cnt = 0;

    for ( int i = 0; i < n; i++ ) {
        scanf ("%d %d %d", &a, &b, &c);

        if (a + b + c >= 2) cnt++;
    }

    printf ("%d\n", cnt);

    return 0;
}

// @END_OF_SOURCE_CODE

D – Hungry Sequence


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cmath>

using namespace std;

bool isPrime(int n)
{
    if ( n < 2 ) return false;

    if (n == 2) return true;

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

int getNextPrime(int n) {

    n++;

    while(!isPrime(n)) {
        n++;
    }

    return n;
}


int main(int argc, const char * argv[])
{
    int n;

    scanf ("%d", &n);

    bool space = false;

    int prime = 2;

    for ( int i = 0; i < n; i++ ) {
        if (space) printf (" ");
        space = true;

        printf ("%d", prime);

        prime = getNextPrime(prime);
    }

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

D – Hungry Sequence (Alternate solution)


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;


int main(int argc, const char * argv[])
{
    int n;

    scanf ("%d", &n);

    bool space = false;

    int ans = n;

    for ( int i = 0; i < n; i++ ) {
        if (space) printf (" ");
        space = true;

        printf ("%d", ans++);
    }

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

E – AC Me


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, const char * argv[])
{
    int frequency [26 + 5];

    char input [100000 + 10];

    while(gets(input)) {
        memset(frequency, 0, sizeof frequency);

        int length = strlen(input);

        for (int i = 0; i < length; i++) {
            frequency [input [i] - 'a']++;
        }

        for (int i = 0; i < 26; i++) {
            printf ("%c:%d\n", 'a' + i, frequency [i]);
        }

        printf ("\n");
    }


    return 0;
}

// @END_OF_SOURCE_CODE

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