zoukankan      html  css  js  c++  java
  • [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]

      Robot Instructions 

    You have a robot standing on the origin of x axis. The robot will be given some instructions. Your task is to predict its position after executing all the instructions.

    • LEFT: move one unit left (decrease p by 1, where p is the position of the robot before moving)
    • RIGHT: move one unit right (increase p by 1)
    • SAME AS i: perform the same action as in the i-th instruction. It is guaranteed that i is a positive integer not greater than the number of instructions before this.

    Input 

    The first line contains the number of test cases T (   T$ le$100). Each test case begins with an integer n (   1$ le$n$ le$100), the number of instructions. Each of the following n lines contains an instruction.   

    Output 

    For each test case, print the final position of the robot. Note that after processing each test case, the robot should be reset to the origin.   

    Sample Input 

    2
    3
    LEFT
    RIGHT
    SAME AS 2
    5
    LEFT
    SAME AS 1
    SAME AS 2
    SAME AS 1
    SAME AS 4
    

    Sample Output 

    1
    -5
    

    题目大意:机器人在原点,有3种命令:LEFT坐标减1,RIGHT坐标加1,SAME AS n和第n个命令一样,问最后机器人的坐标。水题不解释!
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<string.h>
     5 #include<cstring>
     6 #include<sstream>
     7 using namespace std;
     8 int main(){
     9     string str;
    10     int T;cin>>T;
    11     getline(cin,str);
    12     while(T--){
    13         int n;cin>>n;
    14         getline(cin,str);
    15         int move[105];
    16         int sum=0;
    17         for(int i=1;i<=n;i++){
    18             getline(cin,str);
    19             if(str[0]=='L')move[i]=-1;
    20             else if(str[0]=='R')move[i]=1;
    21             else{
    22                 string temp=str.substr(8);
    23                 istringstream in(temp);
    24                 int t;
    25                 in>>t;
    26                 move[i]=move[t];
    27             }
    28             sum+=move[i];
    29         }
    30         cout<<sum<<'
    ';
    31     }return 0;
    32 }
    
    
  • 相关阅读:
    读取json中的中文乱码
    Qt新建一个对话框
    Qt窗口全屏
    Qt设置QWidget背景图片
    Qt加载本地图片
    junit5|软断言
    Classpath resource [/data/department/createDepartment.csv] does not exist
    有个用户反馈上传头像失败,分析原因?
    手机扫描二维码的测试用例(转载)
    一次完整的HTTP请求过程是怎么样的呢?【图文详解】(转载)
  • 原文地址:https://www.cnblogs.com/zjutlitao/p/3650843.html
Copyright © 2011-2022 走看看