zoukankan      html  css  js  c++  java
  • 字符串行走得分

    游戏规则,给定一个字符串数组,初始行走方向向右,每经过一个自然数,得分加上自然数,自然数减一,遇到>则行走方向向右,<则行走方向向左,若遇到>或者<号的下一个位置也是大于小于号,则删除当前位置,数组容量减一。若走出边界则游戏终止,输出最终得分。
    输入n,m,q;
    然后输入长度为n的字符串数组,数组中元素为>,<或者自然数,数组中自然数最大不超过m。
    然后输入q对表示左边界和右边界的元素位置,输出得分。

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<sstream>
    #include<stack>
    using namespace std;
    
    string tostring(int a){
    	stringstream ss;
    	ss<<a;
    	string res;
    	ss>>res;
    	return res;
    	
    }
    
    int toint(string & a){
    	stringstream ss;
    	ss<<a;
    	int res;
    	ss>>res;
    	return res;
    }
    
    int search(vector<string>arr,int left,int right){
    	stack<string>st_left;
    	stack<string>st_right;
    	for(int i=right;i>=left;i--){
    		st_left.push(arr[i]);
    	}
    	
    	int res=0;
    	int flag=1;
    	while(true){
    		if(flag==1){
    			if(st_left.empty()){
    				break;
    			}
    			
    			string tmp=st_left.top();
    			if(tmp=="<"){
    				flag=-1;
    				if(!st_right.empty()&&(st_right.top()==">"||st_right.top()=="<"))
    				        st_left.pop();
    				continue;
    			}
    			else if(tmp==">"){
    				flag=1;
    				st_left.pop();
    				if(!st_left.empty()&&(st_left.top()==">"||st_left.top()=="<"))
    				        continue;
    				st_right.push(tmp);
    			}
    			else{
    				if(tmp=="0")
    			   {
    			   	st_left.pop();
    			   	continue;
    			   }
    				res += toint(tmp);
    				st_left.pop();
    				tmp = tostring(toint(tmp)-1);
    				st_right.push(tmp);
    			}
    		}
    		else{
    			if(st_right.empty()){
    				break;
    			}
    			string tmp=st_right.top();
    			if(tmp==">"){
    				flag=1;
    			if(!st_left.empty()&&(st_left.top()==">"||st_left.top()=="<"))
    				    st_right.pop();
    				continue;
    			}
    			else if(tmp=="<"){
    				flag=-1;
    				st_right.pop();
    				if(!st_right.empty()&&(st_right.top()==">"||st_right.top()=="<"))
    				        continue;
    				st_left.push(tmp);
    			}
    			else{
    			   if(tmp=="0")
    			   {
    			   	st_right.pop();
    			   	continue;
    			   }
    				res += toint(tmp);
    				st_right.pop();
    				tmp = tostring(toint(tmp)-1);
    				st_left.push(tmp);
    			}
    		
    		}
    	}
    	return res;
    }
    int main ()
    {
    int n,m,q;
    cin>>n>>m>>q;
    vector<string>arr(n);
    for(int i=0;i<n;i++){
    	cin>>arr[i];
    }
    
    for(int i=0;i<q;i++){
    	int left,right;
    	cin>>left>>right;
    	cout<<search(arr,left-1,right-1)<<endl;
    }
    return 0;
    }
  • 相关阅读:
    Entity SQL 初入
    ObjectQuery查询及方法
    Entity Framework 的事务 DbTransaction
    Construct Binary Tree from Preorder and Inorder Traversal
    Reverse Linked List
    Best Time to Buy and Sell Stock
    Remove Duplicates from Sorted Array II
    Reverse Integer
    Implement Stack using Queues
    C++中const限定符的应用
  • 原文地址:https://www.cnblogs.com/qiuhaifeng/p/11490305.html
Copyright © 2011-2022 走看看