一道抓小偷的OJ题,不用数组实现,遗憾位移以后没有赋值,在此记下

输入一串字符串’\n’结尾,X代表小偷,#代表普通人,数字代表警察,其数字是警察能巡视到的范围。
输出小偷个数,换行。

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
#include <stdio.h>

int printBin(unsigned long num){
while(num != 0){
printf("%d ", num % 2);
num = num >> 1;
}
printf("\n");
}

int main(){
unsigned long line = 0,rangeLine = 0;
int index = 0,range,count = 0;
char tmp;

scanf("%c",&tmp);
while(tmp != '\n'){

if(index < 20){
if(tmp == 'X'){
line = line | (1 << index);
}else if(tmp == '#'){

}else{
range = tmp - '0';

for(int i = 0; i <= range; i++){

rangeLine = rangeLine | (1 << index + i);
rangeLine = rangeLine | (1 << index - i);
}
}

// printBin(line);
// printBin(rangeLine);

index++;
scanf("%c",&tmp);

}else{
while(index >= 10){

if(rangeLine & 1){
if(line & 1){
count++;
}
}
index--;
line = line >> 1;
rangeLine = rangeLine >> 1;
}

}
}

index--;

while(index >= 0){

// printf("%d %d\n",line & 1,rangeLine & 1);
if(rangeLine & 1){
if(line & 1){
count++;
}
}
index--;
line = line >> 1;
rangeLine = rangeLine >> 1;

}

printf("%d",count);
return 0;
}