2013 ACM/ICPC Asia Regional Changsha Online - C  Color Representation Conversion


考查:耐心、细心、基础能力

看懂题目就不成问题了,另外就只有公式装换和精度处理了。

题目给出四个转换公式:

HSV—>RGB

HSL—>RGB

RGB—>HSV

RGB—>HSL

我们要做到的装换模式有 6 种,转换模式方式如下:

HSV—>RGB:直接装换

HSL—>RGB:直接装换

RGB—>HSV:直接转换

RGB—>HSL:直接转换

HSV—>HSL:HSV—>RGB、RGB—>HSL

HSL—>HSV:HSL—>RGBRGB—>HSV

#include<iostream> #include<string> #include<cstdio> #include<algorithm> #include<math.h> using namespace std; double G,R,B,H,Shsv,V,Shsl,L; double g,r,b; double c,x,m,min1,max1; double h1; void HSVtoRGB() //不解释,就是HSV模式的表示法装换成RGB的表示法 {     c=V*Shsv*0.0001;     h1=(double)H/60;     double p=h1-((int)h1/2*2)-1;     if(p<0)p=-p;     x=c*(1-p);     if(h1<0)R=0,G=0,B=0;     else if(h1<1)r=c,g=x,b=0;     else if(h1<2)r=x,g=c,b=0;     else if(h1<3)r=0,g=c,b=x;     else if(h1<4)r=0,g=x,b=c;     else if(h1<5)r=x,g=0,b=c;     else if(h1<6)r=c,g=0,b=x;     m=V*0.01-c;     r+=m;     g+=m;     b+=m;     r*=255;     g*=255;     b*=255;     R=r;     G=g;     B=b; } void HSLtoRGB()  //同上 {     double q=2*L*0.01-1;     if(q<0)q=-q;     c=(1-q)*Shsl*0.01;     h1=(double)H/60;     double p=h1-((int)h1/2*2)-1;     if(p<0)p=-p;     x=c*(1-p);     if(h1<0)R=0,G=0,B=0;     else if(h1<1)r=c,g=x,b=0;     else if(h1<2)r=x,g=c,b=0;     else if(h1<3)r=0,g=c,b=x;     else if(h1<4)r=0,g=x,b=c;     else if(h1<5)r=x,g=0,b=c;     else if(h1<6)r=c,g=0,b=x;     m=L*0.01-c*0.5;     r+=m;     g+=m;     b+=m;     r*=255;     g*=255;     b*=255;     R=r;     G=g;     B=b; } void RGBtoHSV()   //同上 {     double h,v,s;     r=(double)R/255;     g=(double)G/255;     b=(double)B/255;     max1=r>b?r:b;     max1=max1>g?max1:g;     min1=r<b?r:b;     min1=min1<g?min1:g;     if(max1==min1)h=0;     else if(max1==r&&g>=b)h=60*(g-b)/(max1-min1);     else if(max1==r&&g<b)h=60*(g-b)/(max1-min1)+360;     else if(max1==g)h=60*(b-r)/(max1-min1)+120;     else if(max1==b)h=60*(r-g)/(max1-min1)+240;     H=h;     if(max1==0)s=0;     else s=1-min1/max1;     s*=100;     Shsv=s;     max1*=100;     V=max1; } void RGBtoHSL()     //同上 {     double h,l,s;     r=(double)R/255;     g=(double)G/255;     b=(double)B/255;     max1=r>b?r:b;     max1=max1>g?max1:g;     min1=r<b?r:b;     min1=min1<g?min1:g;     if(max1==min1)h=0;     else if(max1==r&&g>=b)h=60*(g-b)/(max1-min1);     else if(max1==r&&g<b)h=60*(g-b)/(max1-min1)+360;     else if(max1==g)h=60*(b-r)/(max1-min1)+120;     else if(max1==b)h=60*(r-g)/(max1-min1)+240;     H=h;     l=(max1+min1)*0.5;     if(l==0||max1==min1)s=0;     else if(l>0&&l<=0.5)s=(max1-min1)/(2*l);     else if(l>0.5)s=s=(max1-min1)/(2-2*l);     l*=100;     L=l;     s*=100;     Shsl=s; } int main() {     string s,e;     while(cin>>e)  //输入目标模式名称     {         cin>>s;  //输入转换前模式名字(不同模式输入格式不一样)         if(s=="RGB")         {              cin>>R>>G>>B;              if(e=="HSL")              {                  RGBtoHSL();                  cout<<e;                  printf(" %.0lf %.0lf%% %.0lf%%",H,Shsl,L);              }              else if(e=="HSV")              {                  RGBtoHSV();                  cout<<e;                  printf(" %.0lf %.0lf%% %.0lf%%",H,Shsv,V);              }              else                  cout<<e,printf(" %.0lf %.0lf %.0lf",R,G,B);         }         else if(s=="HSL")         {             scanf("%lf%lf%%%lf%%",&H,&Shsl,&L);             if(e=="RGB")             {                 HSLtoRGB();                 cout<<e;                 printf(" %.0lf %.0lf %.0lf",R,G,B);             }             else if(e=="HSV")             {                 HSLtoRGB();                 RGBtoHSV();                 cout<<e;                 printf(" %.0lf %.0lf%% %.0lf%%",H,Shsv,V);             }             else                 cout<<e,printf(" %.0lf %.0lf%% %.0lf%%",H,Shsl,L);         }         else         {             scanf("%lf%lf%%%lf%%",&H,&Shsv,&V);             if(e=="RGB")             {                 HSVtoRGB();                 cout<<e;                 printf(" %.0lf %.0lf %.0lf",R,G,B);             }             else if(e=="HSL")             {                 HSVtoRGB();                 RGBtoHSL();                 cout<<e;                 printf(" %.0lf %.0lf%% %.0lf%%",H,Shsl,L);              }             else                 cout<<e,printf(" %.0lf %.0lf%% %.0lf%%",H,Shsv,V);         }         cout<<endl;     }     return 0; }  

2013 ACM/ICPC Asia Regional Changsha Online - ETravelby Bike


没什么好说的,轮圈来吧!要是够跑一周的,先跑,后面再把尾数处理下就好了。一天天轮的话,不保证不超时。


不说多了,上代码吧!


#include<iostream> #include<cstdio> #include<string> using namespace std; struct node   //一周之类每天的信息 {     string day;   //时间标记     int time;   //工作时间 }bick[7]={"Monday",8,"Tuesday",8,"Wednesday",8,"Thursday",8,"Friday",8,"Saturday",4,"Sunday",4}; int main() {     string s;     int L;     double speed,sumtime;     int day;     int i,j;     while(cin>>s>>L>>speed)     {         for(i=0;i<7;i++)         if(s==bick[i].day)break;         day=i;         sumtime=(double)L/speed;         if((int)sumtime>=48)         sumtime-=(int)sumtime/48*48;   //每周可以工作48小时         if(sumtime==0)   //刚好n周走完         {             day=i-1;             if(day<0)day=6;         }         else         while(sumtime>0)   //剩余时间,论坛提算         {             if(day>6)day=0;             sumtime-=bick[day].time;             if(sumtime<=0)break;             day++;         }         cout<<bick[day].day<<endl;   //直接输出对应日子标记     }     return 0; }