oj2401: water problem

Questions asked
each person to live in the village all know how expensive things in the city. Everyone living in the city all know how expensive things in the village. Allie decided to use these observations to make money. She will start in a city or village, where to buy cheap stuff, go to the opposite type of place (if she is to start in the city, it is a village, or if you start in the village, it is the city), then where money selling out, buy the next new place something (or the opposite type of thing), and so on. She soon left the city or village, the residents realize how she was deceiving them and was angry with her, so she can not go to the same place twice. Elly she can not predict the actual profit when using this strategy will receive. So she decided to just go to as many places. Of course, she must switch between access to rural and visit the city, but she could not visit the same place twice. The system will provide you with a string position, describing all locations Elly accessible. If the i-th place is the village, the i-th character "V", if the city is, for the "C". According to the rules, she returns the largest number of sites that can be accessed.
Input
multiple sets of data.
Each set of data is a string of length 1 to 100, and only the 'V' and 'C' two characters.
The Output
the Sample the Input
Raw
CVVVC
VVV
VVVVCVV
CVCVCVC
V
the Sample the Output
Raw
5
1
3
7
1
title requires a person to go to the city - rural - urban - rural alternating back and forth and asked to up to several cities.
Because the city can go to a maximum associated with the least number of urban and rural areas, where the judgment conditions
两个数字之间关系 .
n n + 1 and a maximum value 2n + 1;
n and n maximum 2N;
n + S (an arbitrary number) and the maximum value of n 2n + 1;
if 0 which is digital, the maximum value is only one.

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

The complete code

#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;
}
Published 38 original articles · won praise 27 · views 3190

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/104803695