zoukankan      html  css  js  c++  java
  • interview ms1 robert move **

    move 2
    turn right
    move 3
    turn right
    move 6

    初始位置为(0,0),方向为north,求最后的位置。

    string2char:  const char* t = second.c_str();

    string2int:    #include <sstream>   string second;  cin>>second;  int steps;  stringstream(second)>>steps;

    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;  
    
    int main()
    {
        int direction[4] = {0,1,2,3}; //north,east,south,west
        int x_weight[4] = {0,1,0,-1};    //if 0 north then 0,if 1 east then 1
        int y_weight[4] = {1,0,-1,0};
    
        string action;
        string second;
        int x_distance = 0;
        int y_distance = 0;
        
        int direction_present = 0;
        while(cin>>action>>second)
        {
            if(action == "move")
            {
                int steps; 
                stringstream(second)>>steps;
                x_distance += x_weight[direction_present]*steps;
                y_distance += y_weight[direction_present]*steps;
            }
            else
            {
                if(second == "left")
                    direction_present = (direction_present + 3) % 4;
                else
                    direction_present = (direction_present + 1) % 4;
            }
        }
        cout<<x_distance<<","<<y_distance<<endl;
        return 0;
    }
  • 相关阅读:
    私有 composer 包创建
    随机数是如何生成的
    TCP 三次握手的意义
    何为真何为假
    Python流程控制语句详解
    Python类中装饰器classmethod,staticmethod,property,
    函数进阶
    初识函数
    文件操作
    is ==小数据池编码解码
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3828560.html
Copyright © 2011-2022 走看看