oj2401:水题

题目要求
每个住在村庄的人都知道城市里的东西有多昂贵。每个居住在城市的人都知道村庄里的东西有多昂贵。艾莉决定使用这些观察来赚钱。她将在某个城市或村庄开始,在那儿买便宜的东西,去相反类型的地方(如果她是在城市开始,则是村落,或者如果在村庄开始,则是城市),然后在那儿赚钱卖掉,买新下一个地方的东西(还是相反类型的东西),依此类推。她离开城市或村庄后不久,居民意识到她是如何欺骗他们并生她的气,所以她不能两次去同一个地方。Elly无法预测她在使用此策略时将获得的实际利润。因此,她决定只想去尽可能多的地方。当然,她必须在访问乡村和访问城市之间进行切换,而且她可能不会两次访问同一地方。系统会为您提供一个字符串位置,描述Elly可以访问的所有位置。如果第i个地方是村庄,则第i个字符为“ V”,如果是城市,则为“ C”。根据上述规则,返回她可以访问的最大地点数。
Input
多组数据。
每组数据是一个字符串,长度 在 1 到 100 ,且只有 ‘V’ 和 ‘C’ 两个字符。
Output
Sample Input
Raw
CVVVC
VVV
VVVVCVV
CVCVCVC
V
Sample Output
Raw
5
1
3
7
1
题目要求一个人去城市–农村–城市–农村交替来回,问最多能到几个城市。
因为能去城市的最大值与最少的城市和农村数有关,这里的判断条件是
两个数字之间关系
n+1 和 n 最大值2n+1;
n 和 n 最大值2n;
n+s(任意数)和 n 最大值2n+1;
当其中有数字为0时,最大值只能是1.

if (c == 0 || v == 0)
            ans = 1;
        else if (c == v)
            ans = 2 * v;
        else if (c >= v + 1)
            ans = 2 * v + 1;

完整代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int main(void)
{
    string str;
    string a[110];
    int c, v,ans ,t;
    while (cin >> str)
    {
        ans = 0;
        c = 0, v = 0;
        for (int i = 0; i < str.size(); i++)
            if (str[i] == 'C')
                c++;
            else
                v++;
        if (c <= v)
        { 
            t = v;
            v = c;
            c = t;
        }
        if (c == 0 || v == 0)
            ans = 1;
        else if (c == v)
            ans = 2 * v;
        else if (c >= v + 1)
            ans = 2 * v + 1;
        cout << ans << endl;
    }
    return 0;
}
发布了38 篇原创文章 · 获赞 27 · 访问量 3190

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/104803695