Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-30

Matching

| 16:40

問題文, SRM 176

全て同じか、全て違う。

347.03/500

const static string memory[4][3] = {
    { "CIRCLE", "DIAMOND", "SQUIGGLE" },
    { "BLUE", "GREEN", "RED" },
    { "EMPTY", "SOLID", "STRIPED" },
    { "ONE", "THREE", "TWO" } };
    
class Matching {
public:
    vector <string> findMatch(vector <string> first, vector <string> second) {
        vector<string> third(4);
        for (int i = 0; i < 4; i++) 
            third[i] = getSymbol(first[i], second[i], i);
        return third;
    }
private:
    string getSymbol(string f, string s, const int kind) {
        if (f == s) return f;
        if (f > s) swap(f, s);
        if (f == memory[kind][0]) {
            if (s == memory[kind][1]) return memory[kind][2];
            else return memory[kind][1];
        } else {
            return memory[kind][0];
        }
    }
};

RGBColor

| 15:54

問題文, SRM 176

補色を返す。

224.33/250

class RGBColor {
public:
    vector <int> getComplement(vector <int> rgb) {
        vector <int> result(3);
        result[0] = 255 - rgb[0];
        result[1] = 255 - rgb[1];
        result[2] = 255 - rgb[2];
        if (abs(rgb[0]-result[0])<=32 && abs(rgb[1]-result[1])<=32 
                && abs(rgb[2]-result[2])<=32) {
            result[0] = (rgb[0]+128 <= 255) ? rgb[0]+128: rgb[0]-128;
            result[1] = (rgb[1]+128 <= 255) ? rgb[1]+128: rgb[1]-128;
            result[2] = (rgb[2]+128 <= 255) ? rgb[2]+128: rgb[2]-128;
        }
        return result;
    }
};