zoukankan      html  css  js  c++  java
  • HDU-1034(简单模拟)

    Candy Sharing Game

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    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
     
    分析:按照题意模拟下来就行了,注意在每次循环一开始要判断每个是不是奇数。
     
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <sstream>
    10 #include <queue>
    11 #include <typeinfo>
    12 #include <fstream>
    13 #include <map>
    14 #include <stack>
    15 using namespace std;
    16 #define INF 100000
    17 typedef long long ll;
    18 const int maxn=10010;
    19 int a[maxn],tmp[maxn];
    20 int main()
    21 {
    22     int n;
    23     while(scanf("%d",&n)==1){
    24         if(n==0) break;
    25         for(int i=1;i<=n;i++)
    26             scanf("%d",&a[i]);
    27         int same,ans=0,num;
    28         while(1){
    29             same=1;
    30             for(int i=1;i<=n;i++)
    31                 if(a[i]%2!=0) a[i]++;
    32             for(int i=2;i<=n;i++)
    33                 if(a[i]==a[i-1]) {num=a[i];same++;}
    34                 else break;
    35             if(same==n) break;
    36             for(int i=1;i<=n;i++){
    37                 a[i]=a[i]/2;
    38                 tmp[i]=a[i];
    39             }
    40             for(int i=1;i<=n;i++){
    41                 if(i==1) a[i]+=tmp[n];
    42                 else a[i]+=tmp[i-1];
    43             }
    44             ans++;
    45         }
    46         printf("%d %d
    ",ans,num);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    tomcatserver解析(五)-- Poller
    最新版OpenWrt编译教程,解决依赖问题
    操作系统2015(四川大学软件学院)
    Kafka专业监控系统Kafka Eagle:支持kerberos认证,并且对接星环TDH集群
    logstash导出ElasticSearch数据到CSV及同步两套ES的数据研究
    hive通过like方式查询多个值
    hadoop balancer平衡集群各节点数据
    Inceptor命令04-表
    Inceptor命令02-命令使用
    Inceptor命令01-表介绍
  • 原文地址:https://www.cnblogs.com/RRirring/p/4729486.html
Copyright © 2011-2022 走看看