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;
    }
    

      

  • 相关阅读:
    内存初始化
    时钟初始化
    auto,register,static分析
    基本数据类型
    LED驱动简单设计
    核心初始化程序
    核心初始化基本介绍
    链接器脚本
    !带有指针的类和struct赋值的本质
    添加thrust的库后出错
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5023595.html
Copyright © 2011-2022 走看看