Life of xhu

About

HOJ_1557 & HOJ_1595

May 13, 2014

  |   #HOJ

今天有点累,不想写中文翻译了,都很容易看懂。而且都是水题,也不用写思路,直接放代码好了。

1557

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    int n,i;
    double input[1100], ave, sum, res1, res2;
    while(cin >> n && n){
        sum = 0;
        for (i = 0; i < n; i++){
            cin >> input[i];
            sum += input[i];
        }
        ave = sum / n;
        res1 = res2 = 0;
        ave = int(ave * 100 + 0.5) / 100.0;
        for (i = 0; i < n; i++){
            if(input[i] < ave) 
                res1 += ave - input[i];
            else 
                res2 += input[i] - ave;
        }
        cout << '$' << fixed << setprecision(2) << min(res1, res2) << endl;
    }
}  

1595

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

void step(string n, string o, double &x, double &y){
    double num = 0;
    for(int i = n.length() - 1; i >= 0; i--){
        int tmp = n[i] - '0';
        for(int j = 0; j < n.length() - 1 - i; j++)
            tmp = tmp * 10;
        num += tmp;
    }
    double d = sqrt(num * num / 2);
    if(o.length() == 1){
        if(o == "N")
            y += num;
        else if(o == "S")
            y -= num;
        else if(o == "E")
            x += num;
        else if(o == "W")
            x -= num;
    }else{
        if(o[0] == 'N')
            y += d;
        else
            y -= d;
        if(o[1] == 'E')
            x += d;
        else
            x -= d;
    }
}

int main(){
    double x, y;
    int count = 0;
    string num_str, ori_str, input;
    while(cin >> input && input != "END"){
        x = y = 0;
        cout << "Map #" << ++count << endl;
        for(int i = 0; i < input.length(); i++){
            if(input[i] >= '0' && input[i] <= '9'){
                num_str = "";
                for(int j = i; j < input.length(); j++){
                    if(input[j] >= '0' && input[j] <= '9')
                        num_str = num_str + input[j];
                    else{
                        i = j - 1;
                        break;
                    }
                }
            }else if(input[i] >= 'A' && input[i] <= 'Z'){
                ori_str = "";
                for(int j = i; j < input.length(); j++){
                    if(input[j] >= 'A' && input[j] <= 'Z')
                        ori_str = ori_str + input[j];
                    else{
                        i = j - 1;
                        break;
                    }
                }
                step(num_str, ori_str, x, y);
            }
        }
        cout << "The treasure is located at (";
        cout << fixed << setprecision(3) << x << ',';
        cout << fixed << setprecision(3) << y << ")." << endl;
        cout << "The distance to the treasure is " << fixed << setprecision(3) << sqrt(x * x + y * y) << '.' << endl << endl;
    }
}