AcWing-3257. Jump for a jump.

Recently, the little game of Jump is popular all over the country and is loved by many players.

The simplified jump-jump rules are as follows: every time the player jumps from the current square to the next square, the game ends if the player does not jump to the next square.

If you jump to the square but do not jump to the center of the square, you get 1 point; when you jump to the center of the square, if the previous score was 1 point or this is the first jump in the game, the score this time is 2 points. Otherwise, the score this time is two points more than the previous score (that is, the total score will be +2, +4, +6, +8...) when jumping to the center of the square continuously.

Now give the whole process of a person jumping and jumping, please find his score in this game (according to the rules described in the title).
Input format The
input contains multiple numbers, separated by spaces, each number is one of 1, 2, 0, 1 means that the jump jumped to the square but not to the center, 2 said that the jump jumped to the square and Jump to the center of the block, 0 means that the jump did not jump to the block (the game is over).
Output format
Output an integer, the score of this game (under the rules of this question).
Data range
For all evaluation cases, no more than 30 numbers are entered, and 0 is guaranteed to appear exactly once and is the last number.
Input sample:
1 1 2 2 2 1 1 2 2 0
Output sample:
22

simulation

#include<bits/stdc++.h>
using namespace std;
const int N=35;
int a[N];
int main()
{
    
    
    for(int i=1;;i++)
    {
    
    
        cin>>a[i];
        if(a[i]==0)
        break;
    }
    int i=1;
    int ans;
    int res=0;
    while(a[i]!=0)
    {
    
    
        if(a[i]==1)
        {
    
    
            res++;
            ans=1;
        }
        else
        {
    
    
            if(ans==1||i==1)
            {
    
    
                res+=2;
                ans=2;
            }
            else
            {
    
    
                res+=ans+2;
                ans+=2;
            }
        }
        i++;
    }
    cout<<res<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51768569/article/details/113924302