[CCF]-One Jump (Analysis)

Description of the problem
  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 it 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...).
  Now give a person the whole process of 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 did not jump 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
  outputs an integer, the score of this game (under the rules of this question).
Sample input

1 1 2 2 2 1 1 2 2 0

Sample output

22

Data scale and conventions
  For all evaluation cases, the number entered should not exceed 30, and 0 should appear exactly once and be the last number.

It is necessary to judge the first number, because this determines whether it is home 1 or plus 2 (if you jump to the center of the block for the first time, then plus 2), and this also ensures that subsequent judgments on consecutive 2 will not An array index out of range occurred. We must pay attention to the sentence "total score will be +2, +4, +6, +8...", that is, he is not simply +2 on the basis of the previous sum, but has to traverse forward, there is a 2, The sum is +2, until there is no, this is the operation of +2, +4, +6, +8... for a continuous 2. Think about it
#include <iostream>
#include <vector>
using namespace std;

int main(){
    
    
	vector<int> v;
	int num;
	while(scanf("%d",&num)&&num!=0){
    
    
		v.push_back(num);
	}
	int sum = 0;
	for(int i = 0;i<v.size();i++){
    
    
		if(i==0){
    
    
			if(v[0]==1)
				sum += 1;
			else
				sum += 2;
		}
		else{
    
    
			if(v[i]==2&&v[i-1]==1)
				sum += 2;
			else if(v[i]==2&&v[i-1]==2){
    
    
				for(int j = i;v[j]==2;j--){
    
    
					sum += 2;
				}
			}
			else
				sum += 1;
		}
	}
	cout << sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108556358