校OJ: P1460 霍金的轮椅

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Elvis_code_t/article/details/79901335

一道模拟题,按照题目的意思一步一步翻译就好了,代码写的有点冗长qwq

#include <stdio.h>

int ans1, ans2;
char ch;
char str[1000];
int cur, x, y;

int main() {
    int i;
	scanf("%d %d %c", &x, &y, &ch); getchar();
	if(ch == 'N') cur = 1;
	else if(ch == 'W') cur = 2;
	else if(ch == 'S') cur = 3;
	else if(ch == 'E') cur = 4;
	gets(str);
	for( i = 0; str[i] != '\0'; i++ ) {
		ch = str[i];
		if( ch == 'G' ) {
			if( cur == 1 ) y++;
			else if( cur == 2 ) x--;
			else if( cur == 3 ) y--;
			else if( cur == 4 ) x++;
		}
		else if( ch == 'R' ) {
			if( cur == 1 ) cur = 4;
			else if( cur == 2 ) cur = 1;
			else if( cur == 3 ) cur = 2;
			else if( cur == 4 ) cur = 3;
		}
		else if( ch == 'L' ) {
			if( cur == 1 ) cur = 2;
			else if( cur == 2 ) cur = 3;
			else if( cur == 3 ) cur = 4;
			else if( cur == 4 ) cur = 1;
		}
	}
	printf("%d %d ", x, y);
	if( cur == 1 ) printf("N");
	else if( cur == 2 ) printf("W");
	else if( cur == 3 ) printf("S");
	else if( cur == 4 ) printf("E");
	return 0;
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页