我正在用阵列解决3个问题。其中两个我已经解决了,我有两个问题。
如果前两个的代码是好的,以及如何解决第三个问题。
好的,第三个问题是以这种方式将数组复制到另一个数组:arrayA = [1,2,3],然后arrayB是[1,2,3,3,2,1]。
首先从开始到结束然后从结束开始。第一个问题是以这种方式将arrayA复制到arrayB:arrayA = [1,2,3],arrayB = [1,1,2,2,3, 3]。 我只会发布我的伪代码。
//object constructor
function cell(){
this.alive = Math.random() > 0.7;
this.neighbours = 0; //number of live neighbours
this.checkneighbours = [[-1,-1],[-1,0],[0,-1],[-1,1],[1,-1],[1,0],[0,1],[1,1]];
}
function GoL(size){
this.size = size;
this.grid = this.makeGrid(size);
};
GoL.prototype.makeGrid = function(size){
var grid = [];
for(var i=0; i<size; i++){
var row=[];
for(var j =0; j<size; j++){
row.push(new cell());
}
grid.push(row);
}
return grid;
};
GoL.prototype.drawGrid = function(){
for(var i=0;i<this.size;i++){
var row =this.grid[i];
var rowCell="";
for(var j=0;j<this.size;j++){
var cell = row[j];
if(cell.alive){
rowCell += "X|";
}else{
rowCell += " |";
}
}
console.log(rowCell);
}
};
GoL.prototype.underpopulation = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours <2){
return true;
}else{
return false;
}
};
GoL.prototype.overpopulation = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours >3){
return true;
}else{
return false;
}
};
GoL.prototype.backtolife = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours ===3 && !cell.alive){
return true;
}else{
return false;
}
};
GoL.prototype.update = function(ro,col){
var cell = this.grid[ro][col];
cell.num_of_neighbours = 0;
for(var i =0; i<this.checkneighbours.length; i++){
var checkneighbour = this.checkneighbours[i];
var neighbour1 = direction[0];
var neighbour2 = direction[1];
if(neighbour1>=0 && neighbour1 < this.size && neighbour2 >=0 && neighbour2 < this.size){
var currentneighbour = this.grid[ro + neighbour1][col+neighbour2];
if(currentneighbour.alive){
cell.num_of_neighbours++;
}
}
}
};
GoL.prototype.updateAll = function(){
for(var i=0; i<this.size;i++){
for(var j=0; j<this.size;j++){
this.update(i,j);
}
}
}
GoL.prototype.cellstatus = function(ro,col){
var cell = this.grid[ro][col];
if(this.underpopulation(ro,col) || this.overpopulation(ro,col)){
cell.alive = false;
}else if(this.backtolife(ro,col)){
cell.alive = true;
}
};
GoL.prototype.allcellstatus = function(ro,col){
for(var i=0; i<this.size;i++){
for(var j=0; j<this.size;j++){
this.cellstatus(i,j);
}
}
};
var gameoflife = new GoL(40);
var interval = setInterval(function(){
GoL.drawGrid();
GoL.updateAll();
GoL.allcellstatus();
},200);
另一个是将arrayA复制到arrayB两次.arrayA = [1,2,3],arrayB = [1,2,3,1,2,3]。我在这里用于循环:
while i<length(a) do begin
b[j]=a[i];
j+=1;
if (j+1) MOD 2 =0 then i+=1;
end;
答案 0 :(得分:1)
如果预先计算了数量,则避免//112233
for i := 1 to length(a) do begin
b[2 * i - 1] := a[i];
b[2 * i] := a[i];
end;
//123123
l := length(a);
for i := 1 to length(a) do begin
b[i] := a[i];
b[i + l] := a[i];
end;
//123321
l := length(a);
for i := 1 to length(a) do begin
b[i] := a[i];
b[2*l - i + 1] := a[i];
end;
个周期
利用简单的索引算术(我假设所有数组都是基于1的):
database.fetchAllSubscriptionsWithCompletionHandler({subscriptions, error in