【问题标题】:simulation algorithm implementation in C/C++C/C++中的仿真算法实现
【发布时间】:2017-09-18 16:18:49
【问题描述】:

让一排8000盏灯。最初,只有位于左侧的那个被点亮。 然后,每一秒,都会执行以下操作:如果左边的灯在一秒前点亮,则每个灯都会改变状态(开或关)。最左边的灯一直亮着。该操作是瞬时的。 当右端的灯第一次点亮时,该过程停止。 有多少灯亮着?

我下面的问题实现是假的,你能帮帮我吗?

#include <cstdio>

int t[8001][2];

int main()
{
    t[1][0] = 1;
    t[1][1] = 1;
    int cpt1 = 0, ip = 0;
    while (t[8000][0] != 1 && t[8000][1] != 1)
    {
        ip++; 
        for (int j=2;j<8001;j++)
        {
            if(t[j-1][!(ip&1)])
                t[j][(ip & 1)] = !t[j][!(ip & 1)];      
        }
    }   

    for(int j = 1;j < 8001; j++)
        cpt1 += t[j][1];

    printf("cpt=%d\n", cpt1);
}

【问题讨论】:

  • 首先想到的不是8001灯吗?无论如何-“是错误的”并没有告诉我们太多关于它为什么是错误的。包括错误消息或错误的原因。编辑:nvm,你正在使用 1-indexing.....
  • 为什么需要二维数组?这个程序很难读。 更新啊,好的。我想这是为了“双缓冲”。虽然仍然不可读
  • 无论如何。从小数字开始并打印出每个周期的输出以进行调试。如果我理解正确,最左边的灯应该每两个周期“发射”一次,每个周期都会向右传播。
  • #include&lt;cstdio&gt; + [C] 标签 - 嗯。
  • @asterix "但我知道问题的解决方案不是那样。" --> 你期望什么?

标签: c implementation


【解决方案1】:

当左侧不变时,代码缺少更新。

代码简化(基于零的偏移量,使用bool)并在下方更正

#include<stdbool.h>
#include<stdio.h>
#define N 8000

bool t[N][2];

int main(void) {
  t[0][0] = true;
  t[0][1] = true;
  int ip = 0;

  while (t[N - 1][0] == 0 && t[N - 1][1] == 0) {
    ip = !ip;
    for (int j = 1; j < N; j++) {
      if (t[j - 1][!ip]) {
        t[j][ip] = !t[j][!ip];
      } else {
        t[j][ip] = t[j][!ip];  // add
      }
    }
  }

  int cpt1 = 0;
  for (int j = 0; j < N; j++) {
    cpt1 += t[j][1];
  }
  printf("N=%d cpt=%d\n", N, cpt1);
  return 0;
}

输出

N=8000 cpt=2048

【讨论】:

    【解决方案2】:

    以下建议的代码:

    1. 干净编译
    2. 使用 C 头文件而不是 C++ 头文件
    3. 执行所需的操作,但不是最快的算法
    4. 被大量评论

    现在建议的代码:

    #include <stdio.h>
    
    int t1[8000];   // initially all zeros
    int t2[8000];
    
    
    int main( void )
    {
        // setup initial conditions
        int numLitLights = 0;
        t1[0] = 1;
    
    
        // while stop condition not true
        while ( t1[7999] != 1 )
        {
            // make one pass through lamps
            // update values
            for (int j=0; j<7999; j++)
            {
                if( t1[j] )
                {
                    t2[j+1] = ( t1[j+1] )? 0 : 1;
                }
            }
    
            // update original
            for( int j=0; j< 8000; j++ )
            {
                t1[j] = t2[j];
            }
        }
    
        // count lit lamps
        for(int j = 0; j < 8000; j++)
        {
           if( t1[j] )
           {
               numLitLights++;
           }
        }
    
        // output number of lit lamps
        printf( "number of lit lamps: %d\n", numLitLights );
    } // end function: main
    

    结果(点亮的灯数)是

    1024
    

    【讨论】:

    • 你的实现和你的结果 1024 是假的,看 chux 的解决方案
    • @asterix。你可能是对的。请告诉我错误的来源。是逻辑错误还是我对问题参数的误解?
    • 你错过了初始化 t2[0]=1;有了这个,你就有了 2048 年的正确解决方案。
    • @asterix,谢谢,让我感动的总是小事。注意:我最初编写的代码将 t1[0] 和 t2[0] 都初始化为 1 然后我错误地认为我不需要初始化 t2[0] sigh
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2023-03-18
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多