zoukankan      html  css  js  c++  java
  • 12503

      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 thati 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
    

     

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cctype>
    using namespace std;
    int main()
    {
    	int t,n,p,a[105];
    	char s[10];
    	cin>>t;
    	while(t--)
    	{
    		p=0;
    		cin>>n;
    		getchar();
    		for(int i=1;i<=n;i++)
    		{
    			gets(s);
    			if(strlen(s)==4)
    				a[i]=-1;
    			else if(strlen(s)==5)
    				a[i]=1;
    			else if(!isdigit(s[strlen(s)-2]))
    				a[i]=a[s[strlen(s)-1]-'0'];
    			else 
    				a[i]=a[s[strlen(s)-1]-'0'+10*(s[strlen(s)-2]-'0')];
    			p+=a[i];
    		}
    		cout<<p<<endl;
    	}
    	return 0;
    }
  • 相关阅读:
    JS Map的使用
    C# 创建Windows服务
    Oracle 表空间不足解决办法
    C# 企业微信API开发(获取Token,给用户发送消息)
    企业微信API开发笔记
    css实现div不定宽高垂直水平居中解决方案
    前端面试题目大全(附答案)
    移动Web开发
    jquery 给iframe里的元素添加事件
    Jquery实现可拖动进度条demo
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3172229.html
Copyright © 2011-2022 走看看