hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏

时间:2023-03-09 17:17:52
hdu 1033 (bit masking, utilization of switch, '\0' as end of c string)                                                       分类:            hdoj             2015-06-15 21:47    37人阅读    评论(0)    收藏

bit masking is very common on the lower level code.

#include <cstdio>
#include <algorithm> #define MAXSIZE 205 char line[MAXSIZE]; int main() {
//freopen("input.txt","r",stdin);
int x,y, dir; // (dir&3) 0,1,2,3 -- right,up,left,down
char *p;
while(scanf("%s",line)!=EOF) {
puts("300 420 moveto\n310 420 lineto");
x=310, y=420, dir=0;
for(p=line;*p;++p) {
if(*p=='A') { --dir; }
else { ++dir; }
switch(dir&3) {
case 0: x+=10; break;
case 1: y+=10; break;
case 2: x-=10; break;
case 3: y-=10; break;
}
printf("%d %d lineto\n",x,y);
}
puts("stroke\nshowpage");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.