Blue Bridge Cup Exams - ants cold (thinking title)

topic:

Here Insert Picture Description

input Output:

Here Insert Picture Description

analysis:

1. If, after two healthy ants meet, two ants will each turn, we can this situation be seen as two ants have crossed each other, each ant or to maintain the original direction carried forward unchanged.
2, two ants in a cold, when two ants meet, in accordance with previous analyzes, the ants will still be cold advance according to the original direction, but this time, only through another health ant ants cold after that, itself also will be cold.
Consolidation of the above, we assume that the beginning of a cold ants head towards the left, then left cold at this time only ants ants toward the right if there is progress will be infected, we will continue to move forward on the left after infection ants, and then it ant ants will be cold towards the right side of the left forward infections. But if the cold did not ant ants left towards the right way forward, then eventually only its own cold. The same applies to the beginning of why ants head towards the right.

Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const int MAXN = 55;
int num[MAXN],n,inde;
int aa[2];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",&num[i]);
    if(num[1] < 0) inde = 0;  //表示感冒的蚂蚁头朝向左边
    else inde = 1;
    for(int i=2;i<=n;++i)
    {
        if(abs(num[i])<abs(num[1])&&num[i]>0)
            aa[inde]++;
        if(abs(num[i])>abs(num[1])&&num[i]<0)
            aa[1-inde]++;
    }
    if(aa[inde]==0)  
    {
        printf("1\n");
        return 0;
    }
    printf("%d\n",aa[inde]+aa[1-inde]+1);
    return 0;
}

Published 61 original articles · won praise 7 · views 3637

Guess you like

Origin blog.csdn.net/weixin_42469716/article/details/104592576