1595.cpp

来自「这是哈尔滨工业大学acmOJ的源代码」· C++ 代码 · 共 78 行

CPP
78
字号
/*  This Code is Submitted by wywcgs for Problem 1595 on 2005-10-02 at 14:48:09 */ 
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
	double treax, treay, m, dis;
	int step, t = 1;
	int i, flag;
	char dir[4], map[16];
	
	while(1) {
		treax = 0;
		treay = 0;
		while(1) {
			if(scanf("%d", &step) == 0) {
				gets(map);
				return 0;
			} else {
				flag = 0;
				for(i = 0; ; i++) {
					dir[i] = getchar();
					if(dir[i] == ',') {
						dir[i] = 0;
						break;
					} else if(dir[i] == '.') {
						dir[i] = 0;
						flag = 1;
						break;
					}
				}
				if(!strcmp(dir, "N")) {
					treay += step;
				} else if(!strcmp(dir, "S")) {
					treay -= step;
				} else if(!strcmp(dir, "W")) {
					treax -= step;
				} else if(!strcmp(dir, "E")) {
					treax += step;
				} else if(!strcmp(dir, "NW")) {
					m = sqrt((double)step*step/2);
					treay += m;
					treax -= m;
				} else if(!strcmp(dir, "NE")) {
					m = sqrt((double)step*step/2);
					treay += m;
					treax += m;
				} else if(!strcmp(dir, "SW")) {
					m = sqrt((double)step*step/2);
					treay -= m;
					treax -= m;
				} else {
					m = sqrt((double)step*step/2);
					treay -= m;
					treax += m;
				}
				if(flag == 1) {
					break;
				}
			}
		}
		printf("Map #%d\n", t);
		t++;
		if(fabs(treax) < 1e-6){
			treax = 0;
		}
		if(fabs(treay) < 1e-6) {
			treay = 0;
		}
		printf("The treasure is located at (%.3lf,%.3lf).\n", treax, treay);
		dis = sqrt(treax*treax+treay*treay);
		printf("The distance to the treasure is %.3lf.\n\n", dis);
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?