UIU Competitive Programmers contest – 06 Solutions


All solutions written by: Rimon Mostafiz

A – Climbing Worm



#include <bits/stdc++.h>
using namespace std;

int n, u, d;

int solve(int n, int u, int d) {
    int climb = 0, minCnt = 0;
    while(climb < n) {
        climb += u;
        minCnt++;
        if (climb >= n) break;
        climb -= d;
        minCnt++;
    }

    return minCnt;
}

int main() {
    ios_base::sync_with_stdio(0);
    while (cin >> n >> u >> d) {
        if(!n && !u && !d) break;
        cout << solve(n, u, d) << endl;
    }

    return 0;
}

B – Hello World!


#include <bits/stdc++.h>
using namespace std;

int n, Case = 0;

int solve(int n) {
    int cnt = 0, total = 1;
    while(total < n) {
        cnt++;
        total += total;
    }
    return cnt;
}

int main() {
    ios_base::sync_with_stdio(0);
    while (cin >> n) {
        if(n == -1) break;
        cout << "Case " << ++Case << ": " << solve(n) << endl;
    }

    return 0;
}

C – Binary Numbers


#include <bits/stdc++.h>
using namespace std;

int test, n;

void solve(int n) {
    bool space = false;
    for(int i = 0 ; i < 20 ; i++) {
        if(n & 1) {
            if(space) cout << " " << i;
            else { cout << i; space = true; }
        }
        n >>= 1;
    }
    cout << endl;
    return;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> test;
    while (test--) {
        cin >> n;
        solve(n);
    }
    return 0;
}

D – Soundex


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000

string st;
int ans[MAX + 7];

void solve(string st) {
    memset(ans, 0, sizeof ans);
    int x = 1;
    for(size_t i = 0 ; i < st.size() ; i++) {
        if(st[i] == 'A' || st[i] == 'E' || st[i] == 'I' || st[i] == 'O' || st[i] == 'U' || st[i] == 'H' || st[i] == 'W' ||st[i] == 'Y')
            ans[x++] = 0;
        else if(st[i] == 'B' || st[i] == 'F' || st[i] == 'P' || st[i] == 'V')
            ans[x++] = 1;
        else if(st[i] == 'C' || st[i] == 'G' || st[i] == 'J' || st[i] == 'K' || st[i] == 'Q' || st[i] == 'S' || st[i] == 'X' ||st[i] == 'Z')
            ans[x++] = 2;
        else if(st[i] == 'D' || st[i] == 'T')
            ans[x++] = 3;
        else if(st[i] == 'L')
            ans[x++] = 4;
        else if(st[i] == 'M' || st[i] == 'N')
            ans[x++] = 5;
        else if(st[i] == 'R')
            ans[x++] = 6;
    }

    for(int i = 1 ; i < x ; i++) {
        if(ans[i] && ans[i] != ans[i-1]) cout << ans[i];
    }
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    while (cin >> st) {
        solve(st);
    }

    return 0;
}

E – Arbiter Login


#include <bits/stdc++.h>
using namespace std;

int test, Case = 0;
string str1, str2, STR1, str, STR;

string capsLock(string s) {
    string st = s;
    for (size_t i = 0 ; i < s.size() ; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') st[i]-=32;
        if (s[i] >= 'A' && s[i] <= 'Z') st[i]+=32;
    }
    return st;
}

string numLock(string s) {
    string st="";
    for (size_t i = 0 ; i < s.size() ; i++)
        if (s[i] < '0' || s[i] > '9') st += s[i];
    return st;
}

void solve(string str1, string str2) {

    if (str1 == str2) cout << "Login successful.";
    else if (STR1 = capsLock(str1), STR1 == str2) cout << "Wrong password. Please, check your caps lock key.";
    else if (str  = numLock (str1), str  == str2) cout << "Wrong password. Please, check your num lock key.";
    else if (STR  = capsLock (str), STR  == str2) cout << "Wrong password. Please, check your caps lock and num lock keys.";
    else cout << "Wrong password.";


}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> test;
    while(test--) {
        cin >> str1 >> str2;
        cout << "Case " << ++Case << ": ";
        solve(str1, str2);
        cout << endl;
    }
    return 0;
}

Advertisements

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