problem3.cpp

来自「This project asks you to write a program」· C++ 代码 · 共 74 行

CPP
74
字号
#include <iostream>using namespace std;char world[8][8]={{'*',' ',' ','*','*',' ',' ',' '},               {'*',' ','*','*',' ','*','*','*'},               {'*',' ','*','*','*','*',' ','*'},               {'*',' ','*',' ',' ','*',' ','*'},               {' ','*',' ',' ',' ',' ',' ',' '},               {' ','*',' ','*',' ',' ','*',' '},               {'*',' ',' ','*',' ',' ','*',' '},               {'*',' ','*',' ','*',' ',' ','*'}};void generation(){	int i,j;	//copy the world table        char copyworld[8][8];	for(i=0;i<8;i++){                for(j=0;j<8;j++){                        copyworld[i][j]=world[i][j];                }        }		for(i=0;i<8;i++){		for(j=0;j<8;j++){			int neighbor=0;			if((i-1>=0)&&(j-1>=0)&&'*'==copyworld[i-1][j-1])neighbor++;			if((i-1>=0)&&'*'==copyworld[i-1][j])neighbor++;			if((i-1>=0)&&(j+1<8)&&'*'==copyworld[i-1][j+1])neighbor++;			if((j-1>=0)&&'*'==copyworld[i][j-1])neighbor++;			if((j+1<8)&&'*'==copyworld[i][j+1])neighbor++;			if((i+1<8)&&(j-1>=0)&&'*'==copyworld[i+1][j-1])neighbor++;			if((i+1<8)&&'*'==copyworld[i+1][j])neighbor++;			if((i+1<8)&&(j+1<8)&&'*'==copyworld[i+1][j+1])neighbor++;			if('*'==copyworld[i][j]){				if(0==neighbor||1==neighbor||neighbor>3){					world[i][j]=' ';				}			}			else{				if(3==neighbor){					world[i][j]='*';				}			}				}	}}void display(){	cout<<"-----------------\n";	int i,j;	for(i=0;i<8;i++){		for(j=0;j<8;j++){			cout<<"|"<<world[i][j];		}		cout<<"|"<<endl;	}	cout<<"-----------------\n";}int main(){	cout<<"The initial world:\n";	display();		cout<<"After one generation:\n";        generation();	display();	return 0;	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?