201803-1 跳一跳 Java

思路:
一个变量plus记录叠加的数,遇到2就+2

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int next = sc.nextInt();
        int score = 0;
        int plus = 0;
        while(next != 0) {
            if(next == 1) {
                plus = 0;
                score += 1;
            }else if(next == 2){
                plus += 2;
                score += plus;
            }
            next = sc.nextInt();
        }
        sc.close();
        System.out.println(score);
    }

}

猜你喜欢

转载自www.cnblogs.com/yu-jiawei/p/12371068.html
今日推荐