2013 ACM/ICPC Asia Regional Changsha Online – C题 Color Representation Conversion (坑爹模拟题)
题意:给你三种颜色表示模式,RGB,HSV和HSL,实现任意模式之间两两转化。
1.最好别看题目中给的转化公式描述,我觉得叙述的一点也不清楚,看维基百科,把维基百科上的公式一句一句翻译过来就好
2.在公式换算的时候,全部转成小数形式。例如:RGB的取值范围0-255转化成0-1, 即r = R/255.0, g = G/255.0 b = B/255.0……
3.HSL->RGB和HSV->RGB需要四舍五入,而HSV和HSL之间的互相转化需要通过RGB: HSV<=>RGB<=>HSL,这里转RGB的时候不需要四舍五入。
4.对于自己转自己的情况,例如RGB->RGB,直接原样输出。
写的时候脑子不是很清楚,加上WA之后各种瞎改,代码略丑= =凑合着看吧……
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std; const double eps = 1e-; struct node
{
int val[];
bool second;
double ang[];
}; char tar[];
char ori[];
node A, ans; int dcmp( double a )
{
if ( fabs(a) < eps ) return ;
return a < ? - : ;
} int Round( double a )
{
return (int)(a + 0.5);
} void init()
{
scanf( "%s", ori );
//printf("A: ");
char tmp[];
for ( int i = ; i < ; ++i )
{
scanf( "%s", tmp );
sscanf( tmp, "%d", &A.val[i] );
//printf( "%d ", A.val[i] );
}
A.second = false;
//puts("");
return;
} void RGBtoHSL()
{
double r, g, b;
if ( A.second )
{
r = A.ang[];
g = A.ang[];
b = A.ang[];
}
else
{
r = A.val[]/255.0;
g = A.val[]/255.0;
b = A.val[]/255.0;
} double h, l, s; //printf( "rgb: %f %f %f\n", r, g, b );
double maxi = max( max(r, g), b );
double mini = min( min(r, g), b ); double delta = maxi - mini;
if ( dcmp( maxi - mini ) == )
{
h = 0.0;
}
else if ( dcmp( maxi - r ) == )
{
if ( dcmp( g - b ) >= )
{
h = 60.0*(g-b)/delta+;
}
else
{
h = 60.0*(g-b)/delta+;
}
}
else if ( dcmp( maxi - g ) == )
{
h = 60.0*(b-r)/delta+;
}
else if ( dcmp( maxi - b ) == )
{
h = 60.0*(r-g)/delta+;
} l = 0.5*(maxi+mini);
//printf("l:%f\n", l); if ( dcmp(l) == || dcmp( delta ) == )
{
s = 0.0;
}
else if ( dcmp(l) > && dcmp( 0.5 - l ) >= )
{
s = delta/(maxi+mini);
}
else
{
s = delta/(2.0-(maxi+mini));
} //printf( "%s ", tar );
//printf("%.0f %.0f%% %.0f%%\n", h+eps, s*100+eps, l*100+eps );
//printf("%d %d%% %d%%\n", Round(h+eps), Round(s*100+eps), Round(l*100+eps) );
ans.val[] = Round(h);
ans.val[] = Round(s*);
ans.val[] = Round(l*);
return;
} void RGBtoHSV()
{
double r, g, b;
if ( A.second )
{
r = A.ang[];
g = A.ang[];
b = A.ang[];
}
else
{
r = A.val[]/255.0;
g = A.val[]/255.0;
b = A.val[]/255.0;
} double h, v, s; double maxi = max( max(r, g), b );
double mini = min( min(r, g), b ); double delta = maxi - mini;
if ( dcmp( delta ) == )
{
h = ;
}
else if ( dcmp( maxi - r ) == )
{
if ( dcmp( g - b ) >= )
{
h = 60.0*(g-b)/delta+;
}
else
{
h = 60.0*(g-b)/delta+;
}
}
else if ( dcmp( maxi - g ) == )
{
h = 60.0*(b-r)/delta+;
}
else if ( dcmp( maxi - b ) == )
{
h = 60.0*(r-g)/delta+;
} if ( dcmp( maxi ) == )
{
s = ;
}
else s = (double)(maxi-mini)/maxi; v = maxi; //printf( "%s ", tar );
//printf("%f %f %f\n", h+eps, s*100+eps, v*100+eps );
//printf("%d %d%% %d%%\n", Round(h+eps), Round(s*100+eps), Round(v*100+eps) );
ans.val[] = Round(h);
ans.val[] = Round(s*);
ans.val[] = Round(v*); return;
} void jiuzheng( double &tC )
{
if ( dcmp( tC ) < ) tC += 1.0;
if ( dcmp( tC - 1.0 ) > ) tC -= 1.0;
return;
} double GetRGB( double p, double q, double tC )
{
if ( dcmp( 1.0/6.0 - tC ) > )
{
return p + ( (q - p) * 6.0 * tC );
}
else if ( dcmp( 0.5 - tC ) > )
{
return q;
}
else if ( dcmp( 2.0/3.0 - tC ) > )
{
return p + ( (q-p)*4.0 - (q-p)*6.0*tC );
}
else return p;
} void HSLtoRGB()
{
double h, s, l;
double r, g, b;
//int R, G, B; h = A.val[]/360.0;
s = A.val[]/100.0;
l = A.val[]/100.0; if ( dcmp(s) == )
{
r = l;
g = l;
b = l;
}
else
{
double p, q, tR, tG, tB; if ( dcmp( 0.5 - l ) > )
q = l * ( 1.0 + s );
else q = l + s - ( l * s ); p = * l - q; tR = h + 1.0/3.0;
tG = h;
tB = h - 1.0/3.0; jiuzheng( tR );
jiuzheng( tG );
jiuzheng( tB ); //r = GetRGB( p, q, tR )*255.0;
r = GetRGB( p, q, tR );
//g = GetRGB( p, q, tG )*255.0;
g = GetRGB( p, q, tG );
//b = GetRGB( p, q, tB )*255.0;
b = GetRGB( p, q, tB );
} //printf( "%s ", tar );
//printf( "RGB: %d %d %d\n", r, g, b );
A.ang[] = r;
A.ang[] = g;
A.ang[] = b;
A.val[] = Round( r * );
A.val[] = Round( g * );
A.val[] = Round( b * );
//printf("HSLtoRGB: %d %d %d\n", A.val[0], A.val[1], A.val[2] );
for ( int i = ; i < ; ++i )
ans.val[i] = A.val[i]; return;
} void HSVtoRGB()
{
double h, s, v;
int R, G, B;
double r, g, b; h = A.val[];
s = A.val[] / 100.0;
v = A.val[] / 100.0; if ( dcmp(s) == )
{
r = g = b = v;
}
else
{
int hi = (int)floor(h/);
double f = h / - hi;
double p = v * ( - s );
double q = v * ( - f*s );
double t = v * ( - (-f)*s ); switch( hi )
{
case : r = v, g = t, b = p; break;
case : r = q, g = v, b = p; break;
case : r = p, g = v, b = t; break;
case : r = p, g = q, b = v; break;
case : r = t, g = p, b = v; break;
case : r = v, g = p, b = q; break;
}
} R = Round( r * 255.0 );
G = Round( g * 255.0 );
B = Round( b * 255.0 ); //printf( "%s ", tar );
//printf( "%d %d %d\n", R, G, B );
A.ang[] = r;
A.ang[] = g;
A.ang[] = b; A.val[] = R;
A.val[] = G;
A.val[] = B;
for ( int i = ; i < ; ++i )
ans.val[i] = A.val[i];
return;
} void HSVtoHSL()
{
HSVtoRGB();
A.second = true;
RGBtoHSL();
return;
} void HSLtoHSV()
{
HSLtoRGB();
A.second = true;
RGBtoHSV();
return;
} int main()
{
while ( scanf( "%s", tar ) == )
{
init();
if ( ori[] == 'R' )
{
if ( tar[] == 'L' ) RGBtoHSL();
else if ( tar[] == 'R' )
{
for ( int i = ; i < ; ++i )
ans.val[i] = A.val[i];
}
else RGBtoHSV();
}
else if ( ori[] == 'L' )
{
if ( tar[] == 'R' ) HSLtoRGB();
else if ( tar[] == 'L' )
{
for ( int i = ; i < ; ++i )
ans.val[i] = A.val[i];
}
else HSLtoHSV();
}
else
{
if ( tar[] == 'R' ) HSVtoRGB();
else if ( tar[] == 'V' )
{
for ( int i = ; i < ; ++i )
ans.val[i] = A.val[i];
}
else HSVtoHSL();
} printf( "%s ", tar );
if ( tar[] == 'R' )
{
printf( "%d %d %d\n", ans.val[], ans.val[], ans.val[] );
}
else printf( "%d %d%% %d%%\n", ans.val[], ans.val[], ans.val[] );
}
return ;
}
2013 ACM/ICPC Asia Regional Changsha Online – C题 Color Representation Conversion (坑爹模拟题)的更多相关文章
- 2013 ACM/ICPC Asia Regional Changsha Online - C Color Representation Conversion
这个纯粹是一个细节题啊!!! 由于某个地方的浮点数比较写错了,WA了无数次啊…… 代码如下: #include<iostream> #include<cstdio> #incl ...
- 2013 ACM/ICPC Asia Regional Changsha Online G Goldbach
比赛的时候,被题目误导了,题目最后说结果可能很大,要取模,那时就想直接求会TLE的!!! 赛后才知道,坑啊………… 代码如下: #include<iostream> #include< ...
- 2013 ACM/ICPC Asia Regional Changsha Online - G(DP)
第一眼就想到DP,然后想了N久就想不到可以不重算的DP 最后没办法了 先算出来 再去重.. 因为最多只有三个 对于三个来说有三种组合情况 x+y+z, x*y*z, x*y+z 那要么 x,y,z都 ...
- 2013 ACM/ICPC Asia Regional Changsha Online - E
第一个被板刷的题 取余 依次算在周几 #include <iostream> #include<cstdio> #include<cstring> #include ...
- 2013 ACM/ICPC Asia Regional Changsha Online J Candies
AC了,但是不知道为什么,但是恶心的不得了~最近写代码,思路都非常清晰,但是代码各种bug~T.T~说说思路吧:二分~330ms~ 小队友fribbi的思路是离线250msAC~ 预处理solve函数 ...
- 2013 ACM/ICPC Asia Regional Changsha Online–C (模拟)
题目描述 略... 题解 注意控制精度即可....变量全部定义成double,结果round就行....妈蛋....被这题目恶心死了.... 代码: #include <iostream> ...
- 2013 ACM/ICPC Asia Regional Changsha Online - J
原题戳这里. 题意: 有一未知列数a1,a2,a3.....an, 已知s[i]=a[i-1]+a[i]+a[i] (1<i<n) s[1]=a[1]+a[2]; s[n]=a[n-1] ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- ioc 的好文章 转自 GavinJun
https://www.cnblogs.com/fuchongjundream/p/3873073.html
- P1242 新汉诺塔(hanio)
这道题加深了hanio的理解 如果我们要移动第n个盘子.那么就是说,n+1以后(包括n+1)的盘子都已经到位了 #include<iostream> #include<cstdio& ...
- Yarn下分片和分块源代码分析
public class FileSplit extends InputSplit implements Writable { private Path file; private long star ...
- python—命名空间、作用域查找顺序、闭包
名称空间 name space,如下图: x = 1, 1存放在内存中,1 会有一个内存地址,x 则 存放在 name space 里,并同时记录了 1的内存地址, 即 名称空间是存放了变量x与1绑定 ...
- cocos2dx 修改亮度、对比度、色调、饱和度
废话少说,直接修改CCSprite使用的片面着色器ccShader_PositionTextureColor_noMVP.frag: /* * cocos2d for iPhone: http://w ...
- HDU2837 Calculation(扩展欧拉定理)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- MySQL工作经验
以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...
- udp回显客户端发送的数据
这里让客户端给服务端发送的数据被服务端自动发回来 客户端: import socket client_socket = socket.socket(socket.AF_INET, socket.SOC ...
- nuxt generate静态化后回退问题
之前线上的项目是nuxt build后的项目发布在服务器上,pm2来管理node的进程,nuxt还是运行在node的环境里. 这个方案用了半年左右,访问速度什么的确实很快,pm2管理下的node在wi ...
- 【word】html转doc的小研究
html转doc,页眉页脚丢失 html 转 doc,是全屏铺满(缩放级别很高)