Compete搭配:单元格竞争问题(amcat excel questions and answers)

关于Compete搭配的问题,在amcat excel questions and answers中经常遇到, 这是我的任务:

这是我的任务:

有一个由 8 个细胞组成的菌落排列成一条直线,每天每个细胞都与它的相邻细胞 (邻居) 竞争。每天,对于每个细胞,如果它的邻居都活跃或都不活跃,细胞在第二天变得不活跃,否则它在第二天变得活跃。

假设:两端的两个小区只有一个相邻的小区,所以可以假设另一个相邻的小区总是不活动的。即使在更新小区状态之后。考虑其先前状态来更新其他小区的状态。同时更新所有小区的小区信息。

写一个函数 cellCompete,它需要一个 8 元素的整数单元格数组,表示 8 个单元格的当前状态,一个整数天表示要模拟的天数。整数值 1 表示活动单元格,值 0 表示非活动单元格。

Program:

int* cellCompete(int* cells,int days)
{
//write your code here
} 
//function signature ends
Test Case 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
Test Case 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]

这是上面给出的问题语句。我为这个问题编写的代码如下。但是输出与输入相同。

#include<iostream>
using namespace std;
// signature function to solve the problem
int *cells(int *cells,int days)
{   int previous=0;
    for(int i=0;i<days;i++)
    {
        if(i==0)
        {
            if(cells[i+1]==0)
            {
            previous=cells[i];
            cells[i]=0;
        }
        else
        {
            cells[i]=0;
        }       
        if(i==days-1)
        {
            if(cells[days-2]==0)
            {
                previous=cells[days-1];
                cells[days-1]=0;
            }
        else
        {
            cells[days-1]=1;
        }
        }
        if(previous==cells[i+1])
        {
            previous=cells[i];
            cells[i]=0;
        }
        else
        {
            previous=cells[i];
            cells[i]=1;
        }
    }
            }
return cells;
}
int main()
{
    int array[]={1,0,0,0,0,1,0,0};
    int *result=cells(array,8);
    for(int i=0;i<8;i++)
    cout<<result[i];
}

我无法得到错误,我认为我的逻辑是错误的。

3
private List<Integer> finalStates = new ArrayList<>();
public static void main(String[] args) {
    // int arr[] = { 1, 0, 0, 0, 0, 1, 0, 0 };
    // int days = 1;
    EightHousePuzzle eightHousePuzzle = new EightHousePuzzle();
    int arr[] = { 1, 1, 1, 0, 1, 1, 1, 1 };
    int days = 2;
    eightHousePuzzle.cellCompete(arr, days);
}
public List<Integer> cellCompete(int[] states, int days) {
    List<Integer> currentCellStates = Arrays.stream(states).boxed().collect(Collectors.toList());
    return getCellStateAfterNDays(currentCellStates, days);
}
private List<Integer> getCellStateAfterNDays(List<Integer> currentCellStates, int days) {
    List<Integer> changedCellStates = new ArrayList<>();
    int stateUnoccupied = 0;
    if (days != 0) {
        for (int i1 = 0; i1 < currentCellStates.size(); i1++) {
            if (i1 == 0) {
                changedCellStates.add(calculateCellState(stateUnoccupied, currentCellStates.get(i1 + 1)));
            } else if (i1 == 7) {
                changedCellStates.add(calculateCellState(currentCellStates.get(i1 - 1), stateUnoccupied));
            } else {
                changedCellStates
                        .add(calculateCellState(currentCellStates.get(i1 - 1), currentCellStates.get(i1 + 1)));
            }
        }
        if (days == 1) {
            System.out.println("days ==1 hit");
            finalStates = new ArrayList<>(changedCellStates);
            return finalStates;
        }
        days = days - 1;
        System.out.println("Starting recurssion");
        getCellStateAfterNDays(changedCellStates, days);
    }
    return finalStates;
}
private int calculateCellState(int previousLeft, int previousRight) {
    if ((previousLeft == 0 && previousRight == 0) || (previousLeft == 1 && previousRight == 1)) {
        // the state gets now changed to 0
        return 0;
    }
        // the state gets now changed to 0
    return 1;
}
3

这是我在 Java 中的解决方案:

public class Colony
{
  public static int[] cellCompete(int[] cells, int days)
  {
    int oldCell[]=new int[cells.length];
    for (Integer i = 0; i < cells.length ; i++ ){
        oldCell[i] = cells[i];
    }
    for (Integer k = 0; k < days ; k++ ){
        for (Integer j = 1; j < oldCell.length - 1 ; j++ ){
            if ((oldCell[j-1] == 1 && oldCell[j+1] == 1) || (oldCell[j-1] == 0 && oldCell[j+1] == 0)){
                cells[j] = 0;
            } else{
                cells[j] = 1;
            }
        }
        if (oldCell[1] == 0){
            cells[0] = 0;
        } else{
            cells[0] = 1;
        }
        if (oldCell[6] == 0){
            cells[7] = 0;
        } else{
            cells[7] = 1;
        }
        for (Integer i = 0; i < cells.length ; i++ ){
            oldCell[i] = cells[i];
        }
    } 
    return cells;
  }
}
2

您的程序不区分模拟的天数和单元格数。

2
#include <bits/stdc++.h>
using namespace std;
int* cellCompete(int* cells,int days)
{
    for(int j=0; j<days; j++)
    {
        int copy_cells[10];
        for(int i=1; i<9; i++)
            copy_cells[i]=cells[i-1];
        copy_cells[0]=0;copy_cells[9]=0;
        for(int i=0; i<8; i++)
            cells[i]=copy_cells[i]==copy_cells[i+2]?0:1;
    }
    return cells;
}
int main()
{
    int arr[8]={1,1,1,0,1,1,1,1};
    int arr2[8]={1,0,0,0,0,1,0,0};
    cellCompete(arr2,1);
    for(int i=0; i<8; i++)
    {
        cout<<arr2[i]<<" ";
    }
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(48)
Canfd协议:无状态协议和有状态协议(stateless protocol)
上一篇
远程连接centos7:在CentOS7上启用Firewalld后 我无法连接
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(11条)