颜色反转

题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3340&konwledgeId=40

解题思路: 直接模拟。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int getOrder(char ch)
 5 {
 6     if (isdigit(ch)) return ch-'0';
 7     return ch-'A'+10;
 8 }
 9 
10 char getChar(int x)
11 {
12     if (x<10) return '0' + x;
13     return 'A' + x - 10;
14 }
15 
16 int char2int(char *s)
17 {
18     return getOrder(s[0])*16 + getOrder(s[1]);
19 }
20 
21 void int2char(int x, char *s)
22 {
23     int i = x / 16;
24     int j = x % 16;
25     s[0] = getChar(i);
26     s[1] = getChar(j);
27 }
28 
29 int main()
30 {
31     char s[10];
32     while (scanf("%s", s) != -1)
33     {
34         int x = char2int(s+1);
35         int2char(255-x,s+1);
36         x=char2int(s+3);
37         int2char(255-x,s+3);
38         x=char2int(s+5);
39         int2char(255-x,s+5);
40         printf("%s\n",s);
41     }
42     return 0;
43 }

 

posted @ 2018-05-04 22:58  只会一点暴力  阅读(645)  评论(0编辑  收藏  举报