?? problem3.cpp
字號:
#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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -