【问题标题】:How to iterate over the series: 0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 7, 8, 8, 9 … without using recursion?如何在不使用递归的情况下迭代系列:0、1、2、3、4、4、5、6、7、7、8、7、8、8、9……?
【发布时间】:2012-10-19 22:21:43
【问题描述】:

我正在试图弄清楚如何在 C 中生成这个数字序列。

0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9 …

序列是由如下图所示的数字组成的三角形生成的:

0
1 2
3 4 5
6 7 8 9 ...

接下来的两个数列的位置如下:

  1. 下一个数字位于正下方
  2. next to next 位于右侧一个位置。

0
|\
1 2

Series -> 0, 1, 2

0
|\
1 2
|\|\
3 4 5

Series -> 0, 1, 2, 3, 4, 4, 5, ........

如何遍历这个数字三角形以在 C 中得到这个序列?

这意味着 0 替换为 1 和 2 1 替换为 3 和 4 2 替换为 4 和 5

0
|\
1 2
|\|\
3 4 5
|\|\|\
6 7 8 9

Series -> 0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 7, 8, 8, 9 ........

意思是

我。解决0

0 leads to 1 and 2

0 -> 1 - 2
0 1 2

二。解决1和2

1 leads to 3 and 4

1 -> 3 - 4
0 1 2 3 4

2 leads to 4 and 5

2 -> 4 - 5
0 1 2 3 4 4 5

三。求解 3、4、4、5

3 leads to 6 and 7

3 -> 6 - 7
0 1 2 3 4 4 5 6 7

4 leads to 7 and 8

4 -> 7 - 8
0 1 2 3 4 4 5 6 7 7 8

4 leads to 7 and 8

4 -> 7 - 8
0 1 2 3 4 4 5 6 7 7 8 7 8

5 leads to 8 and 9

5 -> 8 - 9
0 1 2 3 4 4 5 6 7 7 8 7 8 8 9

我很抱歉没有正确解释。希望这次能解释一下。

【问题讨论】:

  • 按照你的解释,标题中的系列对7,8的配对是不是太多了?
  • 为什么是6, 7, 7, 8, 7, 8, 8, 9 而不是6, 7, 7, 8, 8, 9
  • 下一行是什么? 10,11,11,12,11,12,11,12,12,13,12,13,12,13,13,14, 或 10,11 ,11,12,12,13,11,12,12,13,11,12,12,13,13,14,(粗体表示不同)?我的回答会产生第一个,但你的描述也适合第二个。
  • 请再次查看我编辑过的问题。希望这次可以理解...
  • 到目前为止您尝试过什么?请发布您的代码。

标签: c sequence series


【解决方案1】:

我假设(根据描述)你的序列确实应该是

 0
 1  2
 3  4  4  5
 6  7  7  8  8  9
10 11 11 12 12 13 13 14

等等

你可以用这样的代码来处理它:

int nextRowStart = 0;
int nextRowSize = 1;

for (int curr = 0; /*put some ending condition here*/; curr++)
{
    yield(curr)
    if (curr != nextRowStart - 1 && curr != nextRowStart)
        yield(curr);
    if (curr == nextRowStart)
    {
        nextRowStart += nextRowSize;
        nextRowSize++;
    }
}

void yield(int x)
{
    printf("%d ", x);
}

有了改变的问题,新的问题可以递归完成

这是 C# 中的解决方案:

IEnumerable<int> Generate(int level)
{
    if (level == 0)
    {
        yield return 0;
        yield break;
    }

    int diff = level;
    foreach (int n in Generate(level - 1))
    {
        yield return n + diff;
        yield return n + diff + 1;
    }
}

var result = Enumerable.Range(0, maxLevel).SelectMany(Generate);

翻译成C语言需要一些时间...


C 解决方案:

void Generate(int level, int* resultsize, int** result)
{
    if (level == 0)
    {
        *result = (int*)malloc(sizeof(int));
        (*result)[0] = 0;
        *resultsize = 1;
    }
    else
    {
        int recResultSize;
        int* recResult;
        Generate(level - 1, &recResultSize, &recResult);
        *resultsize = recResultSize * 2;
        *result = (int*)malloc(*resultsize * sizeof(int));
        for (int i = 0; i < recResultSize; i++)
        {
            (*result)[2*i]     = recResult[i] + level;
            (*result)[2*i + 1] = recResult[i] + level + 1;
        }
        free(recResult);
    }
}

【讨论】:

  • 请再次查看我编辑过的问题。希望这次可以理解...
  • @Lusion:刚刚为您的案例添加了 C# 解决方案,我将尝试将其映射到 C 代码。
  • 我已经通过递归对其进行了评估,如果可以使用循环以非递归方式完成它会非常有帮助。谢谢
  • @Lusion:嗯,非递归解决方案需要研究序列的属性,而递归解决方案直接对应您的描述——这就是它更简单的原因:)
  • 查看我的非递归解决方案。
【解决方案2】:

这是给出准确结果并解决问题的代码。就在@Lundin 要求我发布我的代码时,我得到了这个结果,我再次尝试并且成功了。谢谢大家。

#include<stdio.h>

int in;

int main(){
 int  ik, it, icount = 0, ih, temp, ig = 1;
 int aisum[100];
     aisum[0] = 0;
     scanf("%d",&in);
     printf("0\n");
     it = 1;ih = 0;temp = 2;
     for(icount = 0,ig = 1; icount <= in; icount++){
                for(ik = 0; ik<2; ik++){
                        aisum[ig] = aisum[icount] + it + ik ;
                        printf("%d ",aisum[ig]);
                        ig++;
                }

                if(aisum[icount] == ih){
                    printf("\n");
                    it++;
                    ih += temp;
                    temp++;
                }
     }

 return 0;
}
     /*Input the number of elements to be processed*/
     /*icount will account for new elements to be formed like if we look
     at pascal triangle
     0 will form 1 and 2
     1 will form 3 and 4
     */
     /*ig will account for array indices*/
     /*it will account for the periodic addition */
     /*ih checks for the codnition for it to increement
     0
     1 2
     3 4 5
     it will be increemented at 0, 2, 5 ...
     */

【讨论】:

  • 这应该不错,我看到的唯一问题是aisum数组的静态分配。您实际上可以预先计算其所需的大小(考虑到每行大小是先前大小的两倍,它是 2^maxlevel)并使用 malloc 进行分配。
【解决方案3】:

我可以从下面的三角形到所需顺序给出的最简单的解决方案是......

0

1 2

3 4 5

6 7 8 9

打印每行的startend节点并打印中心元素各2次...

对于每一行的开始将等于前一个结束+1...

end 将等于 end+1+count...

每行计数将增加 1...

CPP 计划:

#include<iostream>    
using namespace std;    
int main()    
{    
    int start=1,end=2,count=1;    
    cout<<0<<" ";    
    while(count<5)    
    {    
        cout<<start<<" ";    
        for(int i=start+1;i<end;i++)    
        {    
            cout<<i<<" "<<i<<" ";    
        }    
        cout<<end<<" ";    
        count++;    
        start=end+1;    
        end=start+count;    
    }    
    return 0;    
}    

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 2011-09-08
    • 2021-05-30
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多