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; }