蓝桥0707-探测器上下浮动

#include <cstdio> 
#include <cstring>
#include <iostream> 
#include <stdio.h>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    int h,s;
    cin >> h >> s;
    string co;
    cin >> co;
    int cur = s;
    for(int i = 0;i<co.length();i++)
    {
        if(co[i] == 'u')
        {
            cur -= 1;
        }
        if(co[i] == 'd')
        {
            cur += 1;
        }
        if(cur <= 0)
        {
            cur = 0; 
        } 
        else if(cur >= h)
        {
            cur = h;
        } 
    }
    cout << cur;
    return 0;    
} 
#include <iostream>
using namespace std;

int main(){
    int h,s;
    string str;
    cin>>h>>s>>str;
    for(int i=0;i<str.size();i++){
        if(str[i]=='u'){
            if(s>0){
                s--;
            }
        }else{
            if(s<h){
                s++;
            }
        }
    }
    cout<<s;
}
原文地址:https://www.cnblogs.com/leesymbol/p/13969879.html