Problem - 1034 Candy Sharing Game

本文介绍了一个关于学生间传递糖果的游戏算法。学生们围成一圈,在老师的指挥下将手中的糖果传递给右侧的同学,直到所有人的糖果数量相同。文章提供了实现这一过程的代码示例,并逐步优化以提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题描述如下

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个 小朋友围成一个圈,每个小朋友手上有a [ i ] (初始值为偶数)个糖果,一次循环内,把自己一半的糖果给自己右手边的人,给完和收到之后如果自己的糖果数是奇数,老师会给他一个补成偶数个,知道所有的小朋友手上的糖果数目相等。


思路: 用两个数组a,b分别表示小朋友手上的糖果数目和要给别人的糖果数目,第i个小朋友自己的一半加左边给的一半就是一次循环后的拥有的糖果数目,依次类推。

#include <stdio.h>
int main( )
{
    int i, n, round = 0;
     while(scanf("%d", &n) != EOF && n)
    {
        int  a[100];
        int  b[100];
        int flag = 1;
        for ( i = 0; i < n; i++)
                    scanf("%d",&a [ i ] );          //数组a用来表示i个学生手上有多少个苹果
        while ( flag )
        {
            flag = 0;
            for ( i = 1; i < n; i++ )          //用来判断是否所有学生手上的数目相等
                    if ( a [ i ] != a [ 0 ] )
                    {
                        flag = 1;                          //表示所有学生的糖果数目不等
                        break;
                    }
              if ( i == n )
                        break;

            for ( i = 0; i < n; i++ )                             //开始分配
                {
                    a [ i ] = a [ i ] / 2;
                    b[ i ] = a [ i ] ;
                }
                for ( i= 0; i < n ; i++)
                {
                    if ( i == 0 )                                       //第一个小孩由最后一个给
                        a [ i ] = a [ i ] + b [ n-1 ];
                    else
                        a [ i ] = a [ i ] + b [ i-1 ];
                }
               for ( i = 0; i < n; i++)                  //分配完之后如果是奇数就加一
               {
                   if ( a [ i ] % 2 != 0 )
                   a [ i ] ++;
               }
            round ++;

            }
            printf("%d %d\n",round,a[ 0 ]);
    }
    return 0;
}


不过提交结果是时间超限。。。。。。是因为开了两个数组又用了太多for循环吗。。。。。

然后精简了一下代码,发现其实flag不要也可以。。。把判断是否为奇数的循环直接放到分配循环里这样就省掉了一次循环。同时不用数组,动态调用内存。

#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main( )
{
    int i, n,round = 0;
     while(scanf("%d", &n) != EOF && n)
    {
        int * a = (int *)malloc(n * sizeof(int));
        int * b = (int *)malloc(n * sizeof(int));
        for ( i = 0; i < n; i++)
                    scanf("%d",&a [ i ] );          //数组a用来表示i个学生手上有多少个苹果
        while ( 1 )
        {
            for ( i = 1; i < n; i++ )          //用来判断是否所有学生手上的数目相等
                    if ( a [ i ] != a [ 0 ] )        //表示所有学生的糖果数目不等
                        break;

              if ( i == n )
                        break;

            for ( i = 0; i < n; i++ )                             //开始分配
                {
                    a [ i ] = a [ i ] / 2;
                    b[ i ] = a [ i ] ;
                }
                for ( i= 0; i < n ; i++)
                {
                    if ( i == 0 )                                       //第一个小孩由最后一个给
                        a [ i ] = a [ i ] + b [ n-1 ];
                    else
                        a [ i ] = a [ i ] + b [ i-1 ];
                    if ( a [ i ] % 2 != 0 )
                        a [ i ] ++;
                }
            round ++;
            }
            printf("%d %d\n",round,a[0]);
            free( a );
            free ( b );
    }
    return 0;
}


结果。。。WA了。。。因为round的定义放在最外面了。。。。。

#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main( )
{
    int i, n;
     while(scanf("%d", &n) != EOF && n)
    {
        int round = 0;
        int * a = (int *)malloc(n * sizeof(int));
        int * b = (int *)malloc(n * sizeof(int));
        for ( i = 0; i < n; i++)
                    scanf("%d",&a [ i ] );          //数组a用来表示i个学生手上有多少个苹果
        while ( 1 )
        {
            for ( i = 1; i < n; i++ )          //用来判断是否所有学生手上的数目相等
                    if ( a [ i ] != a [ 0 ] )        //表示所有学生的糖果数目不等
                        break;

              if ( i == n )
                        break;

            for ( i = 0; i < n; i++ )                             //开始分配
                {
                    a [ i ] = a [ i ] / 2;
                    b[ i ] = a [ i ] ;
                }
                for ( i= 0; i < n ; i++)
                {
                    if ( i == 0 )                                       //第一个小孩由最后一个给
                        a [ i ] = a [ i ] + b [ n-1 ];
                    else
                        a [ i ] = a [ i ] + b [ i-1 ];
                    if ( a [ i ] % 2 != 0 )
                        a [ i ] ++;
                }
            round ++;
            }
            printf("%d %d\n",round,a[0]);
            free( a );
            free ( b );
    }
    return 0;
}

以上。

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值