hduoj 1034「建议收藏」

hduoj 1034「建议收藏」CandySharingGameTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):5890    AcceptedSubmission(s):3580ProblemDescriptionAnumberof

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Candy Sharing Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5890    Accepted Submission(s): 3580

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

Jetbrains全家桶1年46,售后保障稳定

 

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<algorithm> #include<iostream> #include<cstring> #include<cmath> using namespace std; int cake[10010]; int n; int pan() {  int t = cake[0];  for(int i = 1;i < n;i++)   {    if(t != cake[i])    {     return 1;    }   }   return 0; } int main() {    while(cin >> n&&n)  {   for(int i = 0;i < n;i++) cin >> cake[i];   int ans = 0;   while(pan())   {    ans++;    int tem = cake[n-1]/2;    for(int i = n-1;i > 0;i--)    {     cake[i] = cake[i]/2+cake[i-1]/2;     if(cake[i] % 2) cake[i]++;    }    cake[0] = cake[0]/2 + tem;    if(cake[0] % 2) cake[0]++;   }   cout << ans << " " << cake[0] << endl;  }  return 0;
}
比比较
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234351.html原文链接:https://javaforall.net

(0)
上一篇 2025年6月6日 下午6:01
下一篇 2025年6月6日 下午6:43


相关推荐

  • STM32 看门狗和嘀嗒定时器

    STM32 看门狗和嘀嗒定时器看门狗一个定时器,独立的定时器,对单片机CPU进行监控,一旦CPU的程序出现错误,或者电压过低使单片机出现任何意外情况,看门狗就会给单片机复位使单片机回到初始状态。单片机就会从错误中脱离出来。看门狗–是一个定时器,供能–计数。每隔一段时间就喂狗–计数清零,重新计时,程序出错不能喂狗,得复位。独立看门狗独立看门狗是基于一个12位的递减计数器和一个8位的预分频器。他有一个内部独立的40KHz的RC振荡器提供时钟;因为这个RC振荡器独立于主时钟,所以他可运行于停机和待机模式。它可以被当成看门狗用于在发生问

    2022年5月26日
    48
  • 【Qt5.12】Qt5.12安装教程[通俗易懂]

    【Qt5.12】Qt5.12安装教程[通俗易懂]00.目录文章目录00.目录01.软件下载02.软件安装03.软件测试04.附录01.软件下载Qt5.12下载网址:http://download.qt.io/archive/qt/5.12/5.12.2/选择Windows平台,Linux和Mac平台类似下载好之后的安装包:02.软件安装Step1:双击安装包,稍等片刻,然后点击nextStep2:…

    2022年5月13日
    117
  • ELF文件格式简介「建议收藏」

    ELF文件格式简介「建议收藏」  简单了解下ELF文件的格式。1简介  可执行与可链接格式(ExecutableandLinkableFormat,ELF),常被称为ELF格式,是一种用于可执行文件、目标代码、共享库和核心转储(coredump)的标准文件格式,一般用于类Unix系统,比如Linux,Macox等。ELF格式灵活性高、可扩展,并且跨平台。比如它支持不同的字节序和地址范围,所以它不会不兼容某一特别的CPU或指令架构。这也使得ELF格式能够被运行于众多不同平台的各种操作系统所广泛采纳。  E.

    2025年7月30日
    5
  • sqlserver语句创建表格_创建表的sql语句外键

    sqlserver语句创建表格_创建表的sql语句外键今天介绍一下如何使用SQLServer语句创建表并添加数据首先先了解一下表的模式,在数据库中根据模式进行分组避免表名称的冲突在SQLServer2014中直接新建表是默认的前缀dbo而命名其他的模式需要使用SQLServer语句进行创建下面将一步一步的进行演示,首先是创建一个数据库然后创建模式在后面使用根据创建的模式或者使用默认的模式名,进行创建表,语句如下图下面解释一下句子的意思看一下新建好的表后面介绍如何在新表里面添加数据…

    2022年8月31日
    4
  • 数据分析模型篇—PEST分析

    数据分析模型篇—PEST分析今天给大家分享的是在企业战略管理中一个比较经典的分析—PEST分析。PEST分析一种企业所处宏观环境分析模型,宏观环境又称一般环境,是指影响一切行业和企业的各种宏观力量。而针对PEST,指的就是P政治(Politics),E经济(Economy),S社会(Society),T技术(Technology),这四类因素一般不受企业掌控,但是又会对企业的发展产生很大的影响,所以PEST分析在很多企业…

    2022年6月12日
    36
  • oracle clob 类型条件,Oracle中Clob类型如何处理?

    oracle clob 类型条件,Oracle中Clob类型如何处理?Oracle 中 Clob 类型处理解析最近利用 NHibernate 映射类型为 Clob 字段在插入数据时发现当字符的字节数 一个半角字符一个字节 一个全角字符两个字节 在 2000 4000 之间时报错 ORA 01461 仅可以插入 LONG 列的 LONG 值赋值 经过不断查找资料和自己的试验该问题终于得到解决 下边我将自己的心得给大家做一个分享 准备系统环境 xp net2 0 oracle9i 表结构 由

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号