java期末考试——自动细胞机

题目描述

mooc链接:

https://www.icourse163.org/learn/ZJU-1001542001?tid=1458169441#/learn/examOj?eid=1216333001&id=1232864406

代码

代码在网上搜过其他同学的代码,但感觉太有点结构化编程的意思了,因此自己写了一个,大概一两个小时,代码应该还能凑合着看

整体

import java.util.Scanner;

public class vendingMachine {
    private int times;
    private int width;
    private int length;

    private int newData[][];
    private int oldData[][];
    Scanner in=new Scanner(System.in);

    void Init(){
        getWidth();
        getLength();

        oldData = new int[length+2][width+2];
        newData = new int[length+2][width+2];

        //读入活的细胞的位置
        while(true)
        {
            int i=in.nextInt();
            int j=in.nextInt();
            if(i==-1&&j==-1)
                break;
            oldData[i+1][j+1]=1;
            newData[i+1][j+1]=1;
        }
        //读入执行步数
        times = in.nextInt()-1;
    }

    void Run(){
        while(times-->=0)
        {
            for(int i=1;i<length+1;i++)
            {
                for(int j=1;j<width+1;j++)
                {
                    int count=getCount_Neighbour(i,j);
                    //细胞复活
                    if(oldData[i][j]==0){
                        if(count==3)
                            newData[i][j]=1;
                    }
                    //细胞死亡
                    else{
                        if(!(count==2||count==3))
                            newData[i][j]=0;
                    }
                }
            }

            //每次run后要更新old数组
            for (int i = 1; i < length+1; i++)
            {
                for (int j = 1; j < width+1; j++)
                {
                    oldData[i][j] = newData[i][j];
                }
            }

        }

    }

    private int getCount_Neighbour(int i,int j){
        //计算位置为i,j的细胞周围活的细胞的数量
        int temp=0;
        temp+=oldData[i-1][j]+oldData[i+1][j]+oldData[i][j-1]+oldData[i][j+1];
        temp+=oldData[i-1][j-1]+oldData[i-1][j+1]+oldData[i+1][j-1]+oldData[i+1][j+1];

        return temp;
    }

    private void getWidth(){
        int huwu=in.nextInt();
        if(huwu>=3&&huwu<=102)
            this.width=huwu;
        else
            System.out.println("The number must be 2~126");
    }

    private void getLength(){
        int qifei=in.nextInt();
        if(qifei>=3&&qifei<=102)
            this.length=qifei;
        else
            System.out.println("The number must be 2~126");
    }

    private int CountAlive(){
        //计算活着的细胞的数量
        int count=0;
        for (int i = 1; i < length+1; i++)
        {
            for (int j = 1; j < width+1; j++)
            {
                if(newData[i][j]==1)
                    count++;
            }
        }
        return count;
    }

    void Test()
    {
        Init();
        Run();
        System.out.println(CountAlive());
    }

}


测试类


public class Main {
    public static void main(String[] args) {
        vendingMachine city = new vendingMachine();
        city.Test();
    }
}

看到这里了 点个赞吧
祝点赞的人都逢考必过,心想事成(没有道德绑架的意思)

猜你喜欢

转载自blog.csdn.net/m0_46243503/article/details/107342365