zoukankan      html  css  js  c++  java
  • M

    Description

    A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to the neighbor on the right. Any student, who ends up with an odd number of pieces of candy, is given another piece by the teacher. The game ends when all students have the same number of pieces of candy.         Write a program which determines the number of times the teacher blows the whistle and the final number of pieces of candy for each student from the amount of candy each child starts with.        
             
     

    Input

    The input may describe more than one game. For each game, the input begins with the number N of students, followed by N (even) candy counts for the children counter-clockwise around the circle. The input ends with a student count of 0. Each input number is on a line by itself.        
             
     

    Output

    For each game, output the number of rounds of the game followed by the amount of candy each child ends up with, both on one line.        
             
     

    Sample Input

    6 36 2 2 2 2 2 11 22 20 18 16 14 12 10 8 6 4 2 4 2 4 6 8 0
     

    Sample Output

    15 14 17 22 4 8
     

    Hint

     The game ends in a finite number of steps because: 
    1. The maximum candy count can never increase.
    2. The minimum candy count can never decrease.
    3. No one with more than the minimum amount will ever decrease to the minimum.
    4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.


    这是我的代码,我依旧不知道这是为什么!!!! 被自己蠢死了!
    #include <iostream>
    using namespace std;
    int x[1000],y[1000];
    void f(int s[],int n,int k)
    {
        int i,j,a[100][100];
        for(i=0;i<n;i++)a[0][i]=s[i];
        for(j=1;;j++){      
            bool flag=true;
            a[j][0]=a[j-1][0]/2+a[j-1][n-1]/2;
            if(a[j][0]%2!=0)a[j][0]++;
            for(i=1;i<n;i++){
                a[j][i]=a[j-1][i-1]/2+a[j-1][i]/2;
                if(a[j][i]%2!=0)a[j][i]++;
                if(flag==true&&a[j][i]!=a[j][0])flag=false;
            }            
          if(flag==true){
              x[k]=j;
             y[k]=a[j][0];
                break;
          }
        }
    }
    int main()
    {
        int n,k=0;
        while(cin>>n){
            if(n==0)break;
            int s[100],i;
            for(i=0;i<n;i++)cin>>s[i];
            f(s,n,k);
            k++;
        }
        for(int i=0;i<k;i++)
            cout<<x[i]<<" "<<y[i]<<endl;
        //system("pause");
        return 0;
    }



    这是正确代码
    #include<iostream>
    using namespace std;
    const int MAXN=1000;
    int a[MAXN];
    int main()
    {
        int n;
        int i;
        while(cin>>n,n)
        {
            for(i=0;i<n;i++)cin>>a[i];
            int res=0;
            while(1)
            {
                for(i=1;i<n;i++)
                  if(a[i-1]!=a[i]) break;
                if(i>=n) break;
                res++;
                int temp=a[n-1]/2;
                for(i=n-1;i>0;i--)
                {
                    a[i]/=2;
                    a[i]+=a[i-1]/2;
                }   
                a[0]/=2; 
                a[0]+=temp;
                for(i=0;i<n;i++)
                 if(a[i]&1) a[i]++;
            }    
            cout<<res<<" "<<a[0]<<endl;
        }    
        //system ("pause");
        return 0;
    }  
  • 相关阅读:
    2_C语言中的数据类型 (九)逻辑运算符与if语句、switch、条件运算符?、goto语句与标号
    2_C语言中的数据类型 (八)运算符
    Python_sklearn机器学习库学习笔记(七)the perceptron(感知器)
    C++ STL 学习笔记__(7)Set和multiset容器
    2_C语言中的数据类型 (七)printf与scanf
    郑捷《机器学习算法原理与编程实践》学习笔记(第五章 梯度寻优)5.1 最优化与计算复杂度
    机器学习笔记(一)机器学习与数学分析
    2_C语言中的数据类型 (七)类型限定
    郑捷《机器学习算法原理与编程实践》学习笔记(第四章 推荐系统原理)(三)SVD
    [译]Javascript timing事件
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5167279.html
Copyright © 2011-2022 走看看