• 分糖果(模拟)



    标题:分糖果

        有n个小朋友围坐成一圈。老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏:

        每个小朋友都把自己的糖果分一半给左手边的孩子。

        一轮分糖后,拥有奇数颗糖的孩子由老师补给1个糖果,从而变成偶数。

        反复进行这个游戏,直到所有小朋友的糖果数都相同为止。

        你的任务是预测在已知的初始糖果情形下,老师一共需要补发多少个糖果。

    【格式要求】

        程序首先读入一个整数N(2<N<100),表示小朋友的人数。
        接着是一行用空格分开的N个偶数(每个偶数不大于1000,不小于2)
        要求程序输出一个整数,表示老师需要补发的糖果数。

    例如:输入
    3
    2 2 4
    程序应该输出:
    4

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    int a[205];
    int b[205];
    
    int main()
    {
    	
         int n;
         cin>>n;
         for(int t=0;t<n;t++)
         {
         	scanf("%d",&a[t]);
         	b[t]=a[t];
    	 }
    	 for(int t=n;t<2*n;t++)
    	 {
    	 	a[t]=a[t-n];
    	 	b[t]=b[t-n];
    	 	
    	 }
    	 
    	 int sum=0;
    	 
    	 while(1)
    	 {
    	 int flag=0;
    	 for(int t=n-1;t<2*n-1;t++)
    	 {
    	 	a[t]/=2;
    	 	
    	 }
    	 
    	 for(int t=n-1;t<2*n-1;t++)
    	 {
    	 	a[t]+=b[t+1]/2;
    //	 	cout<<a[t];
    	 	
    	 }
    	 
    //	  cout<<endl;
    	 for(int t=n-1;t<2*n-1;t++)
    	 {
    	 	if(a[t]%2==1)
    	 	{
    	 	  a[t]++;
    		  sum++;
    		}
    	 }
    	 a[2*n-1]=a[n-1];
    	 for(int t=n-1;t<2*n;t++)
    	 {
    	 	b[t]=a[t];
    //	    cout<<b[t];
    	 }
    //	 cout<<endl;
    	
    	 for(int t=n;t<2*n-1;t++)
    	 {
    	 	if(b[t]==b[t-1])
    	 	{
    	 		flag++;
    		}
    	 }
    	
    	 if(flag==n-1)
    	 {
    	 	break;
    	 }
    //	 cout<<"S"<<endl;
        }
        cout<<sum<<endl;
    	return 0;
    }
  • 相关阅读:
    智能合约初体验
    安装solidity遇见的问题——unused variable 'returned'
    Clojure学习笔记(二)——函数式编程
    《Java虚拟机并发编程》学习笔记
    Clojure学习笔记(一)——介绍、安装和语法
    Ubuntu配置pyethapp
    no leveldbjni64-1.8 in java.library.path
    Merkle Patricia Tree (MPT) 树详解
    Ubuntu下配置和编译cpp-ethereum客户端
    conda安装python库出现ssl error
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781813.html
Copyright © 2020-2023  润新知