プログラミングコンテスト問題集(7問)

L1-1 人と神

指定された文字列を直接出力するPHP実装

<?php
echo "To iterate is human, to recurse divine.";
?>

L1-2 C言語速習

基本数値計算処理の実装例

#include <iostream>
using namespace std;

void calculate() {
    int total, studied, hours;
    cin >> total >> studied >> hours;
    cout << total - studied * hours << endl;
}

L1-3 日付変換

数値形式の日付をYYYY-MM形式に変換するロジック

#include <cstdio>
using namespace std;

void formatDate() {
    int input;
    cin >> input;
    int century = input / 100;
    
    if(century < 100) {
        if(century < 22) {
            printf("20%02d-%02d", century, input % 100);
        } else {
            printf("19%02d-%02d", century, input % 100);
        }
    } else {
        printf("%d-%02d", century, input % 100);
    }
}

L1-4 価格監視システム

閾値以下の商品に通知を出す処理

#include <iostream>
using namespace std;

void monitorPrices() {
    int items;
    double threshold;
    cin >> items >> threshold;
    
    for(int i = 0; i < items; i++) {
        double price;
        cin >> price;
        if(price < threshold) {
            printf("On Sale! %.1f\n", price);
        }
    }
}

L1-5 感情分析

時間帯別感情状態の評価システム

#include <iostream>
using namespace std;

void analyzeMood() {
    int moods[24];
    for(int i = 0; i < 24; i++) {
        cin >> moods[i];
    }
    
    int hour;
    while(cin >> hour) {
        if(hour < 0 || hour >= 24) break;
        cout << moods[hour] << ' ' << (moods[hour] > 50 ? "Yes" : "No") << endl;
    }
}

L1-6 問題フィルタリング

特定キーワードを含む問題を除外する処理

#include <iostream>
#include <string>
using namespace std;

void filterProblems() {
    int total, skip;
    cin >> total >> skip;
    cin.ignore();
    
    string keyword1 = "qiandao";
    string keyword2 = "easy";
    
    for(int i = 0; i <= total; i++) {
        string content;
        getline(cin, content);
        
        if(content.find(keyword1) == string::npos && 
           content.find(keyword2) == string::npos) {
            if(skip < 0) {
                cout << content << endl;
                return;
            }
            skip--;
        }
    }
    cout << "Wo AK le" << endl;
}

L1-7 統計分析

最小値と最大値の出現回数を計算

#include <iostream>
#include <map>
#include <climits>
using namespace std;

void analyzeData() {
    int count;
    cin >> count;
    map<int, int> frequency;
    int minimum = INT_MAX, maximum = INT_MIN;
    
    for(int i = 0; i < count; i++) {
        int value;
        cin >> value;
        frequency[value]++;
        if(value < minimum) minimum = value;
        if(value > maximum) maximum = value;
    }
    
    cout << minimum << " " << frequency[minimum] << endl;
    cout << maximum << " " << frequency[maximum] << endl;
}

L1-8 数列生成

特殊な乗算ルールによる数列生成

#include <iostream>
#include <vector>
using namespace std;

void generateSequence() {
    int first, second, length;
    cin >> first >> second >> length;
    
    vector<int> sequence;
    sequence.push_back(first);
    sequence.push_back(second);
    int index = 0;
    
    while(sequence.size() < length) {
        int a = sequence[index];
        int b = sequence[index + 1];
        int product = a * b;
        
        if(product >= 10) {
            sequence.push_back(product / 10);
            sequence.push_back(product % 10);
        } else {
            sequence.push_back(product);
        }
        index++;
    }
    
    for(int i = 0; i < length; i++) {
        cout << sequence[i] << (i == length - 1 ? "\n" : " ");
    }
}

L2-1 自動包装システム

スタックを用いた物品処理シミュレーション

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

void simulatePackaging() {
    int tracks, capacity, maxSize;
    cin >> tracks >> capacity >> maxSize;
    
    vector<string> items(tracks + 1);
    for(int i = 1; i <= tracks; i++) {
        cin >> items[i];
    }
    
    stack<char> container;
    string result;
    int command;
    
    while(cin >> command && command != -1) {
        if(command == 0) {
            if(!container.empty()) {
                result += container.top();
                container.pop();
            }
        } else {
            if(!items[command].empty()) {
                if(container.size() >= maxSize) {
                    result += container.top();
                    container.pop();
                }
                container.push(items[command][0]);
                items[command].erase(0, 1);
            }
        }
    }
    cout << result << endl;
}

L2-2 ウイルス変異経路

深さ優先探索による最長変異経路探索

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_NODES = 10010;
vector<int> mutationGraph[MAX_NODES];
bool visited[MAX_NODES];
vector<int> longestPath, currentPath;

void findMutationPath(int node) {
    if(currentPath.size() > longestPath.size()) {
        longestPath = currentPath;
    }
    else if(currentPath.size() == longestPath.size() && currentPath < longestPath) {
        longestPath = currentPath;
    }
    
    for(int next : mutationGraph[node]) {
        currentPath.push_back(next);
        findMutationPath(next);
        currentPath.pop_back();
    }
}

int main() {
    int nodes;
    cin >> nodes;
    
    for(int i = 0; i < nodes; i++) {
        int mutations;
        cin >> mutations;
        for(int j = 0; j < mutations; j++) {
            int target;
            cin >> target;
            visited[target] = true;
            mutationGraph[i].push_back(target);
        }
        sort(mutationGraph[i].begin(), mutationGraph[i].end());
    }
    
    for(int i = 0; i < nodes; i++) {
        if(!visited[i]) {
            currentPath.push_back(i);
            findMutationPath(i);
            break;
        }
    }
    
    cout << longestPath.size() << endl;
    for(int i = 0; i < longestPath.size(); i++) {
        cout << longestPath[i] << (i == longestPath.size() - 1 ? "\n" : " ");
    }
    return 0;
}

L2-3 コードリポジトリ分析

ベクターパターンの出現頻度分析

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

struct CodePattern {
    int frequency;
    vector<int> pattern;
    
    bool operator<(const CodePattern& other) const {
        if(frequency == other.frequency) 
            return pattern < other.pattern;
        return frequency > other.frequency;
    }
};

void analyzeCodebase() {
    int samples, elements;
    cin >> samples >> elements;
    map<vector<int>, int> patternCount;
    
    for(int i = 0; i < samples; i++) {
        vector<int> current;
        for(int j = 0; j < elements; j++) {
            int value;
            cin >> value;
            current.push_back(value);
        }
        patternCount[current]++;
    }
    
    cout << patternCount.size() << endl;
    vector<CodePattern> results;
    for(const auto& entry : patternCount) {
        results.push_back({entry.second, entry.first});
    }
    
    sort(results.begin(), results.end());
    for(const auto& result : results) {
        cout << result.frequency;
        for(int num : result.pattern) {
            cout << " " << num;
        }
        cout << endl;
    }
}

タグ: C++ アルゴリズム データ構造 STL 深さ優先探索

5月26日 08:04 投稿