hdu 1033 (bit masking, utilization of switch, '' 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
310 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
",x,y);
        }
        puts("stroke
showpage");
    }
    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.

原文地址:https://www.cnblogs.com/qeatzy/p/4716232.html