题解 | #Candy Sharing Game#

Candy Sharing Game

https://www.nowcoder.com/practice/33adf53e095440b2823a39146a285b77

//每次循环交换时,若手中糖果为奇数,则多的那一个留给自己。题比较简单,但是题意没有讲清楚
#include "stdio.h"
#include "queue"
using namespace std;
int student[1000];//记录学生的糖果数,从student[1]开始
bool equalJudge(int N){//满足条件返回true
    int candy = student[1];
    for (int i = 1; i <= N; ++i) {
        if (student[i] != candy){
            return false;
        }
    }
    return true;
}

void Exchange(int N,int &count){
    int shadow[1000];//studengt[]的影子数组
    for (int i = 1; i <= N; ++i) {
        shadow[i] = student[i];
    }
    for (int i = 1; i <= N; ++i) {//进行一轮循环
        if (student[i]%2 == 1){
            if (i != 1)
                student[i] = student[i]/2 + shadow[i-1]/2 + 1;
            else
                student[i] = student[i]/2 + shadow[N]/2 + 1;
        } else{
            if (i != 1)
                student[i] = student[i]/2 + shadow[i-1]/2;
            else
                student[i] = student[i]/2 + shadow[N]/2;
        }
    }
    for (int i = 1; i <= N; ++i) {
        if (student[i]%2 == 1)
            ++student[i];
    }
    ++count;
}

int main(){
    int N;int count;//count记录响哨次数
    while (scanf("%d",&N)!=EOF){
        if (N == 0)
            return 0;
        count = 0;
        for (int i = 1; i <= N; ++i) {
            scanf("%d",student+i);
        }
        while (equalJudge(N)!=true){
            Exchange(N,count);
        }
        printf("%d %d\n",count,student[1]);
    }
}

全部评论

相关推荐

看到好多帖子双9都kuku挂,双9都挂,那我还投啥啊
_追梦旅人_:同学考虑我司不,我们正在秋招,可在我主页看岗位,感兴趣可直接投递~
点赞 评论 收藏
分享
星期一的大老师:项目描述 和 技术栈单开一栏;八股文:算法与数据结构,计算机网络一定要写,操作系统不了解可以不写;Linux命令,Git,Docker基础命令和基本使用一定要写,要有实际使用场景的解决经验;项目的八股文上:redis 解决 缓存雪崩,缓存击穿,缓存穿透的解决方案,一个问题的不同方案可以一起用,不需要重复在两个项目写。第二个项目换一个。小厂可以投一投
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务