Ladder Competition Practice Problems Solutions

Basic Input/Output Operations

Simple greeting program that reads a name and outputs a welcome message.

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string name;
    cin >> name;
    cout << "Hello " << name << '\n';
    return 0;
}

Date formatting problem requiring conversion from MM-DD-YYYY to YYYY-MM-DD format.

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

int main() {
    int month, day, year;
    scanf("%d-%d-%d", &month, &day, &year);
    printf("%d-%02d-%02d\n", year, month, day);
    return 0;
}

Search and Validation Algorithms

Sequential search for the number 250 in a stream of integers.

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

int main() {
    int value, position = 0;
    while(true) {
        position++;
        cin >> value;
        if(value == 250) {
            cout << position << '\n';
            exit(0);
        }
    }
    return 0;
}

Sleep detection system checking heart rate and breathing rate thresholds.

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

int main() {
    int count;
    cin >> count;
    
    for(int i = 0; i < count; i++) {
        string name;
        int heart_rate, breath_rate;
        cin >> name >> heart_rate >> breath_rate;
        
        if(heart_rate < 15 || heart_rate > 20 || breath_rate < 50 || breath_rate > 70) {
            cout << name << '\n';
        }
    }
    return 0;
}

Matrix Operations and Game Logic

Matrix multiplication with dimension validation.

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

int main() {
    int rows_a, cols_a, rows_b, cols_b;
    
    cin >> rows_a >> cols_a;
    vector<vector<int>> matrix_a(rows_a, vector<int>(cols_a));
    for(int i = 0; i < rows_a; i++)
        for(int j = 0; j < cols_a; j++)
            cin >> matrix_a[i][j];
            
    cin >> rows_b >> cols_b;
    vector<vector<int>> matrix_b(rows_b, vector<int>(cols_b));
    for(int i = 0; i < rows_b; i++)
        for(int j = 0; j < cols_b; j++)
            cin >> matrix_b[i][j];
    
    if(cols_a != rows_b) {
        cout << "Error: " << cols_a << " != " << rows_b << '\n';
    } else {
        cout << rows_a << ' ' << cols_b << '\n';
        for(int i = 0; i < rows_a; i++) {
            for(int j = 0; j < cols_b; j++) {
                int result = 0;
                for(int k = 0; k < cols_a; k++)
                    result += matrix_a[i][k] * matrix_b[k][j];
                cout << result << " \n"[j == cols_b - 1];
            }
        }
    }
    return 0;
}

Rock-paper-scissors game with guaranteed winning strategy.

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

int main() {
    int interval, counter = 0;
    cin >> interval;
    
    string move;
    cin >> move;
    while(move != "End") {
        counter++;
        if(counter % (interval + 1) == 0) {
            cout << move << '\n';
        } else {
            if(move == "ChuiZi") cout << "Bu\n";
            else if(move == "JianDao") cout << "ChuiZi\n";
            else cout << "JianDao\n";
        }
        cin >> move;
    }
    return 0;
}

Numerical Computation and Data Structures

Finding divisible numbers composed entirely of digit 1.

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

int main() {
    int divisor;
    cin >> divisor;
    
    int number = 1, digits = 1;
    while(number < divisor) {
        number = number * 10 + 1;
        digits++;
    }
    
    while(true) {
        cout << number / divisor;
        number %= divisor;
        if(number == 0) break;
        number = number * 10 + 1;
        digits++;
    }
    cout << ' ' << digits << '\n';
    return 0;
}

Library reading time statistics with proper handling of multiple operations.

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

struct BookRecord {
    bool borrowed = false;
    int start_time, end_time;
};

int main() {
    int days;
    cin >> days;
    
    for(int day = 0; day < days; day++) {
        vector<BookRecord> books(1010);
        int book_id, hours, minutes, total_time = 0, completed_books = 0, current_time = 0;
        char operation, separator;
        
        cin >> book_id >> operation >> hours >> separator >> minutes;
        while(book_id) {
            current_time = hours * 60 + minutes;
            if(operation == 'E' && books[book_id].borrowed) {
                completed_books++;
                total_time += current_time - books[book_id].start_time;
                books[book_id].borrowed = false;
            } else if(operation == 'S') {
                books[book_id].borrowed = true;
                books[book_id].start_time = max(current_time, books[book_id].start_time);
            }
            cin >> book_id >> operation >> hours >> separator >> minutes;
        }
        cout << completed_books << ' ' << (completed_books ? (int)round(total_time * 1.0 / completed_books) : 0) << '\n';
    }
    return 0;
}

Sorting and Graph Algorithms

Social media popularity ranking based on unique likes.

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

int main() {
    int users;
    cin >> users;
    
    map<string, set<int>> user_likes;
    vector<string> user_names(users);
    map<string, double> efficiency;
    
    for(int i = 0; i < users; i++) {
        int like_count;
        string username;
        cin >> username >> like_count;
        user_names[i] = username;
        
        for(int j = 0; j < like_count; j++) {
            int item_id;
            cin >> item_id;
            user_likes[username].insert(item_id);
        }
        int unique_items = user_likes[username].size();
        efficiency[username] = 1.0 * like_count / unique_items;
    }
    
    sort(user_names.begin(), user_names.end(), [&](string a, string b) {
        if(user_likes[a].size() == user_likes[b].size()) 
            return efficiency[a] < efficiency[b];
        return user_likes[a].size() > user_likes[b].size();
    });
    
    for(int i = 0; i < 3; i++) {
        if(i >= user_names.size()) cout << "-";
        else cout << user_names[i];
        cout << " \n"[i == 2];
    }
    return 0;
}

Linked list reordering with address-based traversal.

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int main() {
    int start_address, node_count;
    cin >> start_address >> node_count;
    
    vector<pii> nodes(node_count);
    vector<pii> memory(100100);
    
    for(int i = 0; i < node_count; i++) {
        int address, data, next_addr;
        cin >> address >> data >> next_addr;
        memory[address] = {data, next_addr};
    }
    
    int valid_nodes = 0;
    while(start_address != -1) {
        nodes[valid_nodes++] = {memory[start_address].first, start_address};
        start_address = memory[start_address].second;
    }
    
    for(int left = 0, right = valid_nodes - 1; left <= right; left++, right--) {
        if(left == right) {
            printf("%05d %d -1\n", nodes[left].second, nodes[left].first);
            return 0;
        }
        printf("%05d %d %05d\n", nodes[right].second, nodes[right].first, nodes[left].second);
        printf("%05d %d ", nodes[left].second, nodes[left].first);
        if(left == right - 1) printf("-1\n");
        else printf("%05d\n", nodes[right - 1].second);
    }
    return 0;
}

Graph coloring validation using DFS traversal.

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

int main() {
    int vertices, edges, colors_allowed;
    cin >> vertices >> edges >> colors_allowed;
    
    vector<vector<int>> adjacency(vertices + 1);
    for(int i = 0; i < edges; i++) {
        int node1, node2;
        cin >> node1 >> node2;
        adjacency[node1].push_back(node2);
        adjacency[node2].push_back(node1);
    }
    
    vector<int> vertex_colors(vertices + 1);
    vector<bool> visited(vertices + 1);
    bool valid_coloring = true;
    
    auto dfs_validate = [&](auto& self, int current, int parent, int current_color) -> void {
        visited[current] = true;
        for(int neighbor : adjacency[current]) {
            if(vertex_colors[neighbor] == current_color) valid_coloring = false;
            if(neighbor == parent || visited[neighbor]) continue;
            self(self, neighbor, current, vertex_colors[neighbor]);
        }
    };
    
    int test_cases;
    cin >> test_cases;
    while(test_cases--) {
        valid_coloring = true;
        set<int> used_colors;
        
        for(int i = 1; i <= vertices; i++) {
            cin >> vertex_colors[i];
            used_colors.insert(vertex_colors[i]);
        }
        
        for(int i = 1; i <= vertices; i++)
            dfs_validate(dfs_validate, i, 0, -1);
            
        if(used_colors.size() != colors_allowed) valid_coloring = false;
        cout << (valid_coloring ? "Yes" : "No") << '\n';
        vector<bool>(vertices + 1).swap(visited);
    }
    return 0;
}

Union-find data structure for social group identification.

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

int main() {
    int communities;
    cin >> communities;
    
    set<int> individuals;
    vector<int> parent(10010);
    iota(parent.begin(), parent.end(), 0);
    
    auto find_root = [&](auto& self, int x) -> int {
        return parent[x] == x ? x : parent[x] = self(self, parent[x]);
    };
    
    for(int i = 0; i < communities; i++) {
        int members, first_person;
        cin >> members >> first_person;
        individuals.insert(first_person);
        int root1 = find_root(find_root, first_person);
        
        for(int j = 1; j < members; j++) {
            int person;
            cin >> person;
            individuals.insert(person);
            int root2 = find_root(find_root, person);
            if(root1 != root2) {
                parent[root2] = root1;
            }
        }
    }
    
    int total_people = individuals.size(), group_count = 0;
    for(int i = 1; i <= total_people; i++)
        if(find_root(find_root, i) == i) group_count++;
        
    cout << total_people << ' ' << group_count << '\n';
    
    int queries;
    cin >> queries;
    while(queries--) {
        int person1, person2;
        cin >> person1 >> person2;
        int root1 = find_root(find_root, person1);
        int root2 = find_root(find_root, person2);
        cout << (root1 != root2 ? "N" : "Y") << '\n';
    }
    return 0;
}

Tags: C++ algorithms Data Structures graph theory Union-Find

Posted on Sun, 18 Oct 2026 16:49:01 +0000 by dthomas31uk