AOJ 0176 - What Color?
再びVimで書いた.
フォントサイズが小さい
dkが同じ数値の場合に対応するために逆順に調べました.
#include <iostream> #include <cmath> int hextodec(char h){ int res; if(h >= '0' && h <= '9') res = h - '0'; else res = h - 'a' + 10; return res; } int main(){ int color[8][3] = {{0, 0, 0}, {0, 0, 256}, {0, 256, 0}, {0, 256, 256}, {256, 0, 0}, {256, 0, 256}, {256, 256, 0}, {256, 256, 256}}; std::string name[8] = {"black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white"}; std::string c; while(std::cin >> c, c.compare("0")){ int r = hextodec(c[1]) * 16 + hextodec(c[2]), g = hextodec(c[3]) * 16 + hextodec(c[4]), b = hextodec(c[5]) * 16 + hextodec(c[6]); int min_v = 1000000, min_c; for(int i=7;i>=0;i--){ if(color[i][0] == r && color[i][1] == g && color[i][2] == b){ min_c = i; break; } int t = pow(r-color[i][0], 2) + pow(g-color[i][1], 2) + pow(b-color[i][2], 2); if(min_v > t)min_v = t, min_c = i; } std::cout << name[min_c] << std::endl; } }