POJ1046 Color Me Less 水题 C语言

题目:http://poj.org/problem?id=1046

题目大意:颜色的(R,G,B)类似于三维坐标点,颜色的距离定义同空间点的距离。前16组数是目标颜色,从第17组开始,从目标颜色中找出与每个颜色距离最近的颜色,按照规定格式输出

思路:水题,先把目标颜色存起来,后续的颜色每次输入一次就判读输出就可以了

提交情况: AC 1次

总结:这两天感觉算法学不懂,各种郁闷,于是找水题来做,找找安慰。似乎今年寒假的时候还觉得这个题有点麻烦,现在看完全就是水题。至少比寒假时有进步了,嗯。加油

AC code :

View Code
 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5
6 #define MAXN 20
7 #define INF 0x3333333
8
9 struct COLOR {
10 int R, G, B;
11 }RGB[MAXN];
12
13 double DIS(int i, int R, int G, int B) {
14 double dis = sqrt((double)(RGB[i].R - R) * (RGB[i].R - R) + (RGB[i].G - G) * (RGB[i].G - G) + (RGB[i].B - B) * (RGB[i].B - B));
15 return dis;
16 }
17
18 void Print(int i, int R, int G, int B) {
19 printf("(%d,%d,%d) maps to (%d,%d,%d)\n", R, G, B, RGB[i].R, RGB[i].G, RGB[i].B);
20 }
21
22 void MAP(int R, int G, int B) {
23 int i;
24 double dis;
25 int min = INF;
26 int mark;
27 for(i = 1; i <= 16; ++i) {
28 dis = DIS(i, R, G, B);
29 if(dis < min) {
30 min = dis;
31 mark = i;
32 }
33 }
34 Print(mark, R, G, B);
35 }
36
37 int main() {
38 int i;
39 int R, G, B;
40 for(i = 1; i <= 16; ++i) {
41 scanf("%d%d%d", &RGB[i].R, &RGB[i].G, &RGB[i].B);
42 }
43 while(scanf("%d%d%d", &R, &G, &B)) {
44 if(R == -1 && G == -1 && B == -1)
45 return 0;
46 MAP(R, G, B);
47 }
48 return 0;
49 }
posted @ 2011-07-25 16:48  cloehui  阅读(340)  评论(0编辑  收藏  举报