2013 ACM/ICPC Asia Regional Changsha Online

这个纯粹是一个细节题啊!!!

由于某个地方的浮点数比较写错了,WA了无数次啊……

代码如下:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cstring>
  6 using namespace std;
  7 char str1[10],str2[10];
  8 double h,s,l,r,g,b,v;
  9 bool is(double h,double a,double b)
 10 {
 11     if(h-a>=-1e-8&&h-b<1e-8) return 1; //这里忘记加-,一直是WA……
 12     return 0;
 13 }
 14 void v_r()
 15 {
 16     double c=v*s;
 17     double hh=h/60.0;
 18     double x=c*(1-fabs(fmod(hh,2.0)-1.0));
 19     r=g=b=0;
 20     if(is(hh,0,1))r=c,g=x;
 21     else if(is(hh,1,2)) r=x,g=c;
 22     else if(is(hh,2,3)) g=c,b=x;
 23     else if(is(hh,3,4)) g=x,b=c;
 24     else if(is(hh,4,5)) r=x,b=c;
 25     else if(is(hh,5,6)) r=c,b=x;
 26     double m=v-c;
 27     r+=m;
 28     g+=m;
 29     b+=m;
 30 }
 31 void l_r()
 32 {
 33     double c=(1.0-fabs(2.0*l-1.0))*s;
 34     double hh=h/60.0;
 35     double x=c*(1.0-fabs(fmod(hh,2.0)-1.0));
 36     r=g=b=0;
 37     if(is(hh,0,1))r=c,g=x;
 38     else if(is(hh,1,2)) r=x,g=c;
 39     else if(is(hh,2,3)) g=c,b=x;
 40     else if(is(hh,3,4)) g=x,b=c;
 41     else if(is(hh,4,5)) r=x,b=c;
 42     else if(is(hh,5,6)) r=c,b=x;
 43     double m=l-c*0.5;
 44     r+=m;
 45     g+=m;
 46     b+=m;
 47 }
 48 void r_l_v(char c)
 49 {
 50     double ma=max(max(r,g),b);
 51     double mi=min(min(r,g),b),t=ma-mi;
 52     if(ma-mi<1e-8) h=0;
 53     else if(ma-r<1e-8&&g>=b) h=60*(g-b)/t;
 54     else if(ma-r<1e-8&&g<b) h=60*(g-b)/t+360;
 55     else if(ma-g<1e-8) h=60*(b-r)/t+120;
 56     else if(ma-b<1e-8) h=60*(r-g)/t+240;
 57     l=(ma+mi)*0.5;
 58     v=ma;
 59     if(c=='L'){
 60         if(fabs(l)<=1e-8||fabs(t)<=1e-8) s=0;
 61         else if(l-0.5<=1e-8) s=t/2.0/l;
 62         else s=t/(2-2*l);
 63     }
 64     else{
 65         if(fabs(ma)<1e-8) s=0;
 66         else s=1-mi/ma;
 67     }
 68 }
 69 int main()
 70 {
 71     while(scanf("%s",str1)!=EOF){
 72         scanf("%s",str2);
 73         if(str2[2]=='L'){
 74             scanf("%lf %lf%% %lf%%",&h,&s,&l);
 75             if(str1[2]=='L'){
 76                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,s,l);
 77                 continue;
 78             }
 79             s/=100;l/=100;
 80             l_r();
 81             if(str1[2]=='B') 
 82                 printf("%s %.0lf %.0lf %.0lf
",str1,round(255*r),round(255*g),round(255*b));
 83             else{
 84                 r_l_v('V');
 85                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,round(100*s),round(100*v));
 86             }
 87         }
 88         else if(str2[2]=='V'){
 89             scanf("%lf %lf%% %lf%%",&h,&s,&v);
 90             if(str1[2]=='V'){
 91                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,s,v);
 92                 continue;
 93             }
 94             s/=100;v/=100;
 95             v_r();
 96             if(str1[2]=='B') 
 97                 printf("%s %.0lf %.0lf %.0lf
",str1,round(255*r),round(255*g),round(255*b));
 98             else{
 99                 r_l_v('L');
100                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,round(100*s),round(100*l));
101             }
102         }
103         else if(str2[2]=='B'){
104             scanf("%lf%lf%lf",&r,&g,&b);
105             if(str1[2]=='B'){
106                 printf("%s %.0lf %.0lf %.0lf
",str1,r,g,b);
107                 continue;
108             }
109             r/=255;g/=255;b/=255;
110             if(str1[2]=='L'){
111                 r_l_v('L');
112                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,round(100*s),round(100*l));
113             }
114             else{
115                 r_l_v('V');
116                 printf("%s %.0lf %.0lf%% %.0lf%%
",str1,h,round(100*s),round(100*v));
117             }
118         }
119     }
120     return 0;
121 }
View Code
原文地址:https://www.cnblogs.com/xin-hua/p/3335640.html