首页 > 代码库 > POJ 1473 There's Treasure Everywhere!(简单几何)

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

There‘s Treasure Everywhere!


博客原文地址:http://blog.csdn.net/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;
}


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