50134. 64, 32, 16, and 8
難度:4.5/5
開union必會遇到Little-Endian的問題! 一開始享用union跟memcpy來處理,但遇到Endianess的問題所以卡了很久。警惕自己沒事不要亂開union,能用一般得處理方式最好。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
uint64_t pow_func(uint64_t b, int r){
uint64_t ans = 1;
while(r--) ans *= b;
return ans;
}
static inline void solve_0(){
char cmd[5] = {0};
uint32_t l;
uint16_t s;
uint8_t c1, c2;
while(scanf("%s %u %hu %c %c", cmd, &l, &s, &c1, &c2) != EOF){
uint64_t ans = 0; int ans_ptr = 0, c2_appeared = 0;
for(int i = 3; i >= 0; i--){
if(cmd[i] == 'L'){
ans += pow_func(256, ans_ptr) * (uint64_t)l;
ans_ptr += 4;
}
else if(cmd[i] == 'S'){
ans += pow_func(256, ans_ptr) * (uint64_t)s;
ans_ptr += 2;
}
else if(c2_appeared == 0){
ans += pow_func(256, ans_ptr) * (uint64_t)c2;
ans_ptr += 1;
c2_appeared = 1;
}
else{
ans += pow_func(256, ans_ptr) * (uint64_t)c1;
ans_ptr += 1;
}
}
printf("%llu\n", ans);
}
}
void solve_1(){
char cmd[5] = {0};
uint64_t ans;
while(scanf("%s %llu", cmd, &ans) != EOF){
uint32_t l; uint16_t s; uint8_t c1, c2;
int c2_appeared = 0;
for(int i = 3; i >= 0; i--){
if(cmd[i] == 'L'){
l = (uint32_t)ans;
ans >>= 32;
}
else if(cmd[i] == 'S'){
s = (uint16_t)ans;
ans >>= 16;
}
else if(c2_appeared == 0){
c2 = (uint16_t)ans;
ans >>= 8;
c2_appeared = 1;
}
else{
c1 = (uint16_t)ans;
ans >>= 8;
}
}
printf("%u %hu %c%c\n", l, s, c1, c2);
}
}
int main(){
int task; scanf("%d", &task);
if(task == 0) solve_0();
else solve_1();
}