水下探测器_题解

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     
 7     int h, s;
 8     string str;//或 char str[105]
 9     cin>>h>>s;
10     cin>>str;
11     int len=str.length();  //或len=strlen(str)  注意区分字符数组长度和字符串变量长度的获取方式
12     //cout<<len;// 测试代码
13     for(int i=0; i<len; i++)
14     {
15         if(str[i]=='u'&&s>0)s--;
16         if(str[i]=='d'&&s<h)s++;
17     }
18     cout<<s;
19     return 0;
20 }

此题为简单模拟题,注意边界的判定!

posted @ 2020-07-10 14:28  TFLSNOI  阅读(296)  评论(0编辑  收藏  举报