Soldiers bullet points algorithm to achieve --java

topic

In a particular firing training, the squad will give ten fighters circle bullets. First, to the first monitor 10 soldiers, two soldiers second, third soldier 8, 22 soldiers fourth, fifth soldiers 16, four soldiers sixth, seventh soldier 10, six soldiers eighth, ninth soldiers 14, 20 soldiers tenth. Then follows each fighter in the hands of bullets adjustments: all soldiers checked the number of bullets in their hands, if the number of bullets is odd, to monitor and then to one. Then each soldier while another bullet in their hands to the next half of our soldiers (of 10 soldiers in the hands of bullet points to the first half of a warrior).
After adjustments need to ask how many times the number of bullets in the hands of each soldier are equal? How many bullets per person have?
Each output required number of bullets in the hands of the soldiers adjusted each round.

Thinking

This difficult question in general, is tired, I could not think of a better algorithm. First, there is a very important issue is to test every fighter for equality. If unrestricted, then it will lead to the determination of the O (n ^ 2) may occur, so I'll use a total number of operations for the bullet modulo the number of soldiers, which ensures that only when the value of 0 is where the average score appear, which can greatly reduce the number of cycles. After the operation is relatively common.

Code

public static void fenZiDan(int[]shibing){
        int total = 0;//子弹总数
        int times = 0;//次数
        int[] temp = new int[shibing.length];//临时记录士兵第一次交出子弹后的数量
        for(int i=0;i<shibing.length;i++){
            total+=shibing[i];
        }
        while(true){//循环执行任务
            times++;
            for(int i=0;i<shibing.length;i++){
                if(shibing[i]%2!=0){
                    total++;//向班长要一颗子弹
                    temp[i] = (shibing[i]+1)/2;
                }else {
                    temp[i] =shibing[i]/2;
                }
            }
            for(int i=0;i<shibing.length;i++){
                if(i>0) {
                    shibing[i] = temp[i - 1] + temp[i];
                }else {
                    shibing[i] = temp[i]+temp[temp.length-1];
                }
                if(i!=shibing.length-1){
                    System.out.print(shibing[i]+" ");
                }else {
                    System.out.println(shibing[i]+" ");
                }
            }

            if(total%shibing.length==0){//表示可以平均分,是子弹都相等的必要不充分条件,只有此时才会进行检查
                boolean isEnd = true;
                for(int i=1;i<shibing.length;i++){
                    if(shibing[0]!=shibing[i]){
                        isEnd = false;
                        break;
                    }
                }
                if(isEnd){
                    break;
                }
            }
        }
        System.out.println("总共进行了"+times+"次");
    }
Published 42 original articles · won praise 11 · views 2903

Guess you like

Origin blog.csdn.net/weixin_41746577/article/details/104019999