zoukankan      html  css  js  c++  java
  • Sicily课程练习 1052. Candy Sharing Game

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    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

    题意描述:
    n个小朋友围成圈,每次老师吹哨,小朋友会将手里的candy去下一半交给下一个小朋友,每次这么操作之后,若某个小盆友的candy为奇数,老师给他加一块candy,继续吹哨,直到每个小盆友手中的candy数目相同。输出吹哨的次数和此时每个小盆友手里的candy。多个测试用例,输入第一行为小盆友的个数,接下来分别读入每个小盆友手中初始的candy数量;

    思路:
    开个数组无脑模拟。虽说是无脑模拟,但是对比其他人,我的代码还是太长了orz,毕竟还是太菜了!!出于防止遗忘的目的,这里用了一个静态成员变量,复习一下,而不是全局变量,当然全局也行。纯个人意愿。
    代码+少量注释:
    #include <iostream>
    using namespace std;
    
    class Student{
    public:
        int leftstudent;
        int rightstudent;
        int candy;
        int temp;//用来储存上一次操作之前的candy
        static bool thesame;//静态成员变量,可以通过类名访问,需要在类外初始化
    };
    
    bool Student::thesame = false;
    
    int main(){
        int i, numofstudent;
    
        while(1){
            /*bool*/ Student::thesame = false;//默认为相邻的两个学生的糖果数目相同 
            Student student[1001];
            int rounds_num = 0;
            int flag;
    
            cin >> numofstudent;
            if(numofstudent == 0)
                break;
            
            student[1].leftstudent = numofstudent;//首尾特殊情况,单独拿出来初始化
            student[1].rightstudent = 2;
            student[numofstudent].leftstudent = numofstudent - 1;
            student[numofstudent].rightstudent = 1;
    
            for(i = 2; i < numofstudent; i++){//初始化左右学生的编号
                student[i].leftstudent = i - 1;
                student[i].rightstudent = i + 1;
            }
    
            for(i = 1; i <= numofstudent;i++ ){//初始化糖果的数量
                cin >> student[i].candy;
                student[i].temp = student[i].candy;
            }
           
            while(!(Student::thesame)){
                flag = 0;
                for(i = 1; i <= numofstudent;i++){
                    student[i].candy = student[i].candy / 2 + student[student[i].leftstudent].temp / 2;
                    if(student[i].candy % 2 != 0)
                        student[i].candy++;
                }
                for(i = 1; i <= numofstudent;i++){
                    if(student[i].candy == student[student[i].rightstudent].candy)
                        flag++;
                    student[i].temp = student[i].candy;
                }
                rounds_num++;
    
                if(flag == numofstudent)
                    Student::thesame = true;
            }
    
            cout << rounds_num << " " << student[1].candy << endl;
            
        }
    
          system("pause");
        return 0;
    }


  • 相关阅读:
    python Windows环境下文件路径问题
    pycharm 取消连按两下shift出现的全局搜索
    python2 与 python3的区别
    Python安装PyOpenGL
    Protobuffer学习文档
    python bin文件处理
    python 项目自动生成requirements.txt文件
    pytest文档7-pytest-html生成html报告
    python from __future__ import division
    细说 Java 的深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/nomonoyumei/p/3496814.html
Copyright © 2011-2022 走看看