zoukankan      html  css  js  c++  java
  • 杭电acm 1034题

    Problem 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.
     

     这道题的大意是N个开始都拿着偶数颗糖的小朋友围着老师坐,老师一吹哨子同时给自己右手边的同学自己一半的糖,结束后奇数颗糖的同学老师会给一颗糖补成偶数棵,直到大家手上的糖一样数目...问需要玩几次,以及最后糖的数量是多少?

     1 #include <iostream>
     2 using namespace std;
     3 #define Max 10000
     4 int judge(int *stu,int len);
     5 int main(void)
     6 {
     7     int student[Max];
     8     int N;
     9     int j=0;
    10     int i=0;
    11     int temp[Max];
    12     int around=0;
    13     cin>>N;
    14     while(N)
    15     {
    16         around=0;
    17         for(i=0;i<N;i++)
    18             cin>>student[i];
    19         for(j=0;;j++)
    20         {
    21             
    22             //所有的都看看有没有奇数
    23             for(i=0;i<N;i++)
    24             {
    25                 student[i]=student[i]/2;
    26                 temp[i]=student[i];
    27             }
    28             for(i=1;i<N;i++)
    29             {
    30                 student[i]+=temp[i-1];
    31             }
    32             student[0]+=temp[N-1];
    33             for(i=0;i<N;i++)
    34             {
    35                 if(student[i]%2==1)
    36                     student[i]+=1;
    37             }
    38             around+=1;
    39             if(judge(student,N))
    40                 break;
    41             
    42         }
    43         
    44         cout<<around<<" "<<student[0]<<endl;
    45         cin>>N;
    46     }
    47     return 0;
    48 }
    49 int judge(int *stu,int len)
    50 {
    51     int flag=1;
    52     for(int i=1;i<len;i++)
    53     {
    54         if(stu[i-1]==stu[i])
    55             continue;
    56         else {flag=0;break;} 
    57 
    58     }
    59     return flag;
    60 
    61 }
  • 相关阅读:
    【转载】Java系列笔记(3)
    CentOS 7下Samba服务器的安装与配置
    Linux常用目录结构
    Linux计划任务crontab
    转:Linux 双网卡配置两个IP同时只有一个会通的原因
    centos7中搭建ntp服务器
    centos7中使用vg方式扩充root分区
    ping命令脚本实现显示网络状态、学生姓名、学号
    centos中基于随机数,再加入班级学生姓名
    centos7 shell脚本实现随机数
  • 原文地址:https://www.cnblogs.com/kb342/p/3710089.html
Copyright © 2011-2022 走看看