POJ 1473 There's Treasure Everywhere!(简单几何)

本文介绍了一个字符串处理问题,通过一系列操作(移动距离与方向)确定初始点的最终位置及其与起始点的距离。代码示例清晰展示了算法逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There's Treasure Everywhere!


博客原文地址:http://blog.youkuaiyun.com/xuechelingxiao/article/details/40865611


题目大意:

给你一个字符串,里面有许多的操作,前面的数字是移动的距离,后面的英文表示移动的方向,问最后从远点出发的一个点回落在什么地方以及距离出发点的距离是多少。


解题思路:

题目本身并不是很难,也没有什么坑点,没什么好说的,字符串处理的时候细心一点就行。 

PS:每组后面需要加一个回车,因为这个PE了一次啊啊啊啊!


代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
const double t = sqrt(2.0);

int main()
{
    string s;
    int icase = 1;
    while(cin >> s) {
        if(s == "END") {
            break;
        }
        int num = 0;
        double x = 0, y = 0;
        for(int i = 0; i < (int)s.length()-1; ++i) {
            if(s[i] >= '0' && s[i] <= '9') {
                num = num*10+(s[i]-'0');
            }
            else if(s[i] == ','){
                num = 0;
                continue;
            }
            else if(s[i] == 'N' && s[i+1] == 'E') {
                x+=num/t, y+=num/t, i++;
            }
            else if(s[i] == 'N' && s[i+1] == 'W') {
                x-=num/t, y+=num/t, i++;
            }
            else if(s[i] == 'S' && s[i+1] == 'E') {
                x+=num/t, y-=num/t, i++;
            }
            else if(s[i] == 'S' && s[i+1] == 'W') {
                x-=num/t, y-=num/t, i++;
            }
            else if(s[i] == 'N') {
                y+=num;
            }
            else if(s[i] == 'S') {
                y-=num;
            }
            else if(s[i] == 'E') {
                x+=num;
            }
            else if(s[i] == 'W') {
                x-=num;
            }
            //printf("%lf %lf\n", x, y);
        }
        printf("Map #%d\n", icase++);
        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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值