• 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;
    }


  • 相关阅读:
    中断一个telnet连接
    QQ提示应用程序并行配置不正确,绿色版QQ不能运行解决方法
    新劳动合同法今起实施 正式工与派遣工同工同酬
    最详细的装修施工顺序
    最好的前端hacking备忘录——集锦
    基于vue配置axios
    Vue 实现loading进度条
    Vue 实现countDown倒计时
    使用mongoose操作mongodb数据库
    Vue项目之IE下打开页面是空白
  • 原文地址:https://www.cnblogs.com/nomonoyumei/p/3496814.html
Copyright © 2020-2023  润新知