zoukankan      html  css  js  c++  java
  • Candy Sharing Game(模拟搜索)

    Candy Sharing Game

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

    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

    题解:深搜一下。。。关键要开两个数组;因为自己给下一个一半的时候自己变了,所以再开个数组记录上一状态;

    代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define PI(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define P_ printf(" ")
    #define T_T while(T--)
    typedef long long LL;
    const int INF=0x3f3f3f3f;
    int N;
    int m[110],n[110];
    int ans;
    void dfs(int t){
    	if(ans)return;
    	if(*max_element(m,m+N)==*min_element(m,m+N)){
    		ans=1;
    		printf("%d %d
    ",t,m[0]);
    		return ;
    	}
    	for(int i=1;i<N;i++){
    		m[i]=(n[i-1]+n[i])/2;
    		if(m[i]&1)m[i]++;
    	//	if(m[i]&1)m[i]++;
    	}
    	m[0]=(n[0]+n[N-1])/2;
    	if(m[0]&1)m[0]++;
    	for(int i=0;i<N;i++)n[i]=m[i];
    //	if(m[0]&1)m[0]++;
    	//for(int i=0;i<N;i++)printf("%d ",m[i]);puts("");
    	//getchar();
    	dfs(t+1);
    }
    int main(){
    	while(SI(N),N!=0){
    		for(int i=0;i<N;i++)scanf("%d",&n[i]),m[i]=n[i];
    		ans=0;
    		dfs(0);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    wireshark无法捕获无线网卡数据解决办法(failed to set hardware filter to promiscuous mode)
    用PHP检测用户是用手机(Mobile)还是电脑(PC)访问网站
    一次.net Socket UDP编程的10万客户端测试记录
    对象复制
    c#中volatile关键字的作用
    C#操作XML
    ASP.NET AJAX
    C#操作XMl2
    SQLServer 存储过程中不拼接SQL字符串实现多条件查询
    ASP.NET刷新页面的六种方法20081111 22:04asp.net页面刷新重是有问题,收藏几种方法挺有用的.
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5023595.html
Copyright © 2011-2022 走看看