NOJ - T010
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】NOJ 和 C 程序設計習題
[
23 瀏覽
0 評論
4 贊
4 踩
]
概要
正文
#include <stdio.h> #include <math.h> double max(double _a, double _b, double _c) { double a_b = _a > _b ? _a : _b; return a_b > _c ? a_b : _c; } double min(double _a, double _b, double _c) { double a_b = _a < _b ? _a : _b; return a_b < _c ? a_b : _c; } int main() { int r, g, b; scanf("%d %d %d", &r, &g, &b); // RGB 要轉換爲分量再計算 double lfR = r / 255.0; double lfG = g / 255.0; double lfB = b / 255.0; double lfMax = max(lfR, lfG, lfB); double lfMin = min(lfR, lfG, lfB); double lfRange = lfMax - lfMin; double V = lfMax; double S = (lfMax == 0.0) ? 0.0 : lfRange / lfMax; double H; if(lfRange == 0.0) // 注意考慮分母爲 0 的情況! { H = 0; } else { if (lfMax == lfR) H = 0 + (lfG - lfB) / lfRange; else if (lfMax == lfG) H = 2 + (lfB - lfR) / lfRange; else if (lfMax == lfB) H = 4 + (lfR - lfG) / lfRange; else return -1; H *= 60; if (H < 0) H += 360; } printf("%.4lf,%.4lf%%,%.4lf%%", H, S * 100, V * 100); return 0; }