分糖果:蓝桥杯(java)3151

ackage test;
import java.util.Scanner;


public class assocate_candy {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int arr[] = new int[N];
        int medArr[] = new int[N];
        for(int i = 0; i < N ; i++) {
            arr[i] = sc.nextInt();
        }


        //标记元素
        boolean flag = false;


        int count = 0;

        while(flag == false) {

            flag = true;
            //每次都先将分一半的糖果先整体做出来
            for(int i=0;i=0;i--) {
                //当i等于N-1时
                if(i == N-1) {
                    arr[i] = arr[i]+medArr[0];
                    //老师分给他一颗糖
                    if(arr[i]%2 == 1) {
                        arr[i]++;
                        count++;
                    }
                }else {
                    arr[i] = arr[i]+medArr[i+1];
                    //老师分给他一颗糖
                    if(arr[i]%2 == 1) {
                        arr[i]++;
                        count++;
                    }
                }
            }

        }
        System.out.println(count);
    }
}

你可能感兴趣的:(java,蓝桥杯,算法)