UVa 587 - There's treasure everywhere!

题目:你开始在坐标原点,想去目的地,给你一系列的指路信息,问目的地的位置和到原点的距离。

分析:模拟,计算几何。直接按照顺序计算即可,利用相对坐标求绝对坐标。

说明:注意输入格式。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int    temp;
char   buf[10];
double x,y;

int input(void)
{
	scanf("%d",&temp);
	int count = 0;
	while (buf[count] = getchar()) {
		if (buf[count] == '.' || buf[count] == ',') break;
		count ++;
	}
	int flag = 1;
	if (buf[count] == '.') flag = 0;
	buf[count] = 0;
	return flag;
}

void deal(void)
{
	if (buf[0] == 'W') { x -= temp;return; }
	if (buf[0] == 'E') { x += temp;return; }
	if (buf[0] == 'N') {
		if (buf[1] == 'W') { x -= temp*sqrt(0.5);y += temp*sqrt(0.5);return; }
		if (buf[1] == 'E') { x += temp*sqrt(0.5);y += temp*sqrt(0.5);return; }
		y += temp;return;
	}
	if (buf[0] == 'S') {
		if (buf[1] == 'W') { x -= temp*sqrt(0.5);y -= temp*sqrt(0.5);return; }
		if (buf[1] == 'E') { x += temp*sqrt(0.5);y -= temp*sqrt(0.5);return; }
		y -= temp;return;
	}
}

int main()
{	
	int T = 1,B;
	while ((B = getchar()) != 'E') {
		ungetc(B, stdin);
		x = y = 0.0;
		while (input())
			deal();
		deal();getchar();
		printf("Map #%d\n",T ++);
		printf("The treasure is located at (%.3lf,%.3lf).\n",x,y);
		printf("The distance to the treasure is %.3lf.\n\n",sqrt(x*x+y*y));
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值