| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2162 人关注过本帖
标题:关于一维life game的代码,请看一下错在什么地方
取消只看楼主 加入收藏
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
结帖率:96.88%
收藏
已结贴  问题点数:20 回复次数:5 
关于一维life game的代码,请看一下错在什么地方
#include<stdio.h>
int judge(int a[],int x)
  {   int num=0;
      for(int i=x-2;i=x+2;i++)
      if((i!=x)&&a[i]) num++;
      if(num==2)return 1;
      if((a[x])&&num==4) return 1;
      if(a[x]==0&&num==3)return 1;
      return 0;
  }
  void update(int a[])
  {   int b[62];
      for(int j=0;j<62;j++)
      b[j]=a[j];
      for(int j=1;j<61;j++)
      a[j]=judge(b,j);
  }
void print(int a[])
{
      for(int j=1;j<61;j++){
       if(a[j]) printf("*");
        else printf("-");}
}
int main()
  {   int grid[62];
      for(int i=0;i<62;i++)
      grid[i]=0;
      int x;
      scanf("%d",&x);
      while(x!=-1){
          grid[x]=1;
          scanf("%d",&x);}
      int n;
      scanf("%d",&n);
      for(int i=0;i<n;i++)
          update(grid);
      print(grid);
      return 0;
  }

[此贴子已经被作者于2020-3-9 16:20编辑过]

搜索更多相关主题的帖子: int num grid for return 
2020-03-09 14:37
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
One-Dimensional Life takes place on a straight line instead of a rectangular grid. Each cell has four neighboring positions: those at distance one or two from it on each side. The rules are similar to those of two-dimensional Life except (1) a dead cell with either two or three living neighbors will become alive in the next generation, and (2) a living cell dies if it has zero, one, or three living neighbors. (Hence a dead cell with zero, one, or four living neighbors stays dead; a living cell with two or four living neighbors stays alive.) The progress of sample communities is shown in Figure 1.6(Textbook page44). Design, write, and test a program for one-dimensional Life.
The total count of the cells is less than 60.

Input: the position of living cells (1<=postion<=60. Terminate the list with the special number -1).
      The number (n) of generation. (n=0 means the initial Grid)
Output: the next n generations of the grid.

For example:
【输入】
5 7 -1
1
【输出】
-----*------------------------------------------------------

我想要两颗西柚。
2020-03-09 14:38
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
//这是二维的规则,因为上面一维的规则要参照二维
//老师给了我们二维的标答,但是我仿照二维数组写这串一维数组的代码的时候始终无法输出正确结果,看了好几遍也看不出问题
Definitions:
Life is really a simulation, not a game with players. It takes place on unbounded rectangular grid in which each cell can either be occupied by an organism or not. Occupied cells are called alived; unoccupied cells are called dead. Which cells are alive changes from generation to generation according to the number of neighboring cells that are alive, as follows transition rules:
(1) The neighbors of a given cell are the eight cells that touch it vertically, horizontally, or diagonally.
(2) If a cell is alive but either has no neighboring cells alive or only one alive, then in the next generation the cell dies of loneliness.
(3) If a cell is alive and has four or more neighboring cells also alive, then in the next generation the cell dies of overcrowding.
(4) A living cell with either two or three living neighbors remains alive in the next generation.
(5) If a cell is dead, then in the next generation it will become alive if it has exactly three neighboring cells, no more or fewer, that are already alive. All other dead cells remain dead in the next generation.
(6) All births and deaths take place at exactly the same time.
(7) The size of grid is 20*60

Input: the coordinates of living cells (Terminate the list with the special pair -1 -1).
      The number (n) of generation. (n=0 means the initial Grid)
Output: the next n generations of the grid.

For example:
【输入】
5 3
5 4
5 5
5 6
-1 -1 //输入结束
3 //输出第3代的结果
【输出】
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
---**-------------------------------------------------------
--*--*------------------------------------------------------
---**-------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------

我想要两颗西柚。
2020-03-09 14:39
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
//害,发现了一个点,就是构造的数组超出范围了,修改一下,但还是不对
#include<stdio.h>
#include<string.h>
int judge(int a[],int x)
  {   int num=0;
      for(int i=x-2;i=x+2;i++)
      if((i!=x)&&a[i]) num++;
      if(num==2)return 1;
      if((a[x])&&num==4) return 1;
      if(a[x]==0&&num==3)return 1;
      return 0;
  }
  void update(int a[])
  {   int b[64];
      for(int j=0;j<64;j++)
      b[j]=a[j];
      for(int j=2;j<62;j++)
      a[j]=judge(b,j);
  }
void print(int a[])
{
      for(int j=2;j<62;j++){
       if(a[j]) printf("*");
        else printf("-");}
}
int main()
  {   int grid[64];
      for(int i=0;i<64;i++)
      grid[i]=0;
      int x;
      scanf("%d",&x);
      while(x!=-1){
          grid[x+1]=1;
          scanf("%d",&x);}
      int n;
      scanf("%d",&n);
      for(int i=0;i<n;i++)
          update(grid);
      print(grid);
      return 0;
  }


[此贴子已经被作者于2020-3-9 15:46编辑过]


我想要两颗西柚。
2020-03-09 15:28
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


我的理解就是在某一代中一个活着的点有2或4个活着的邻居,它在下一代就继续活着;死了的点有2或3个活着的邻居,它就复活;
其他情况下下一代的点都是死的

我想要两颗西柚。
2020-03-09 17:08
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
回复 9楼 纯蓝之刃
我想问的是一维的情况QAQ

我想要两颗西柚。
2020-03-09 22:16
快速回复:关于一维life game的代码,请看一下错在什么地方
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018748 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved