如何在不使用数组的情况下使用C语言创建Aitken’s数组?

huangapple go评论65阅读模式
英文:

How to make to make Aitken’s array using C without using array?

问题

以下是翻译好的内容:

Aitken's array(艾特肯数组)是一个由行读取的数字三角形,定义如下:an, k,其中 n ≥ 0,0 ≤ kna0, 0 = 1,an, 0 = an−1, n−1an, k = an, k−1 + an−1, k−1。艾特肯数组的前几行如下:

1
1 2
2 3 5
5 7 10 15
15 20 27 37 52

你尝试的代码如下:

#include <stdio.h>

int main() {
    int i,
        j,
        current = 1,
        next = 1,
        temp;

    printf("%d\n", current);

    for (i = 2; i <= 5; i++) {
        temp = current + next;
        printf("%d ", next);

        for (j = 3; j <= i; j++) {
            current = next;
            next = temp;
            temp = current + next;
            printf("%d ");
        }

        printf("\n");
    }

    return 0;
}

如果你有其他问题,请随时提出。

英文:

How can we display Aitken’s array without using an array?

Aitken’s array is a triangle of numbers {a<sub>n, k</sub>, n ≥ 0, 0 ≤ kn} read by rows, defined by a<sub>0, 0</sub>=1, a<sub>n, 0</sub> = a<sub>n−1, n−1</sub>, a<sub>n, k</sub> = a<sub>n, k−1</sub> + a<sub>n−1, k−1</sub>. The first few rows are:

1
1 2
2 3 5
5 7 10 15
15 20 27 37 52

I tried this :

#include &lt;stdio.h&gt;

int
main()
{
	int i,
	 j,
	 current = 1,
		next = 1,
		temp;

	printf(&quot;%d\n&quot;, current);

	for (i = 2; i &lt;= 5; i++) {
		temp = current + next;
		printf(&quot;%d &quot;, next);

		for (j = 3; j &lt;= i; j++) {
			current = next;
			next = temp;
			temp = current + next;
			printf(&quot;%d &quot;, next);
		}

		printf(&quot;\n&quot;);
	}

	return 0;
}

答案1

得分: 1

Caveat: 不是一个解决方案,但我有模式。

下面,out 是输出行。而 dif 是[给定]输出行的元素之间的差异。

行 1:
	out	1

行 2:
	out	1		2
	dif		1

行 3:
	out	2		3		5
	dif		1		2

行 4:
	out	5		7		10		15
	dif		2		3		5

行 5:
	out	15		20		27		37		52
	dif		5		7		10		15

注意,输出行 3 的差异行与输出行 2 相同(例如 1 2)。

因此,行 X 的差异是行 X-1 的输出行。

英文:

Caveat: Not a solution, but I've got the pattern.

Below, out is the output line. And, dif is the difference between elements of the [given] output line.

Line 1:
	out	1

Line 2:
	out	1		2
	dif		1

Line 3:
	out	2		3		5
	dif		1		2

Line 4:
	out	5		7		10		15
	dif		2		3		5

Line 5:
	out	15		20		27		37		52
	dif		5		7		10		15

Notice that the difference line for output line 3 is the same as the output line for line 2 (e.g. 1 2).

So, the differences for line X is the output line for line X-1.

答案2

得分: 1

以下是代码部分的翻译:

f( r=0, i=0       ) = 1
f( r>0, i=0       ) = f( r-1, r-1 )
f( r>0, i in 1..r ) = f( r, i-1 ) + f( r-1, i-1 )
size_t n = 5;

for ( size_t r = 0; r < n; ++r ) {
   for ( size_t i = 0; i < r; ++i ) {
      printf( " %d ", f( r, i ) );
   }

   printf( " %d\n", f( r, r ) );
}

请注意,以上翻译中已去除代码部分以外的内容。

英文:

The differences between the terms in one row is equal to the terms of the preceding row. As such, the pattern can be defined by the following function where r is the (0-based) row and i is the offset within the row.

f( r=0, i=0       ) = 1
f( r&gt;0, i=0       ) = f( r-1, r-1 )
f( r&gt;0, i in 1..r ) = f( r, i-1 ) + f( r-1, i-1 )

This could easily be implemented as a recursive function which would be called as follows:

size_t n = 5;

for ( size_t r = 0; r &lt; n; ++r ) {
   for ( size_t i = 0; i &lt; r; ++i ) {
      printf( &quot;%d &quot;, f( r, i ) );
   }

   printf( &quot;%d\n&quot;, f( r, r ) );
}

As this sounds like homework, I'll leave the implementation of f to you.


A off-topic note on efficiency. Using recursion (without memoization) is extremely inefficient. A particularly efficient solution requires the use of an array of size n, but we are specifically excluded from using this here.

huangapple
  • 本文由 发表于 2023年4月7日 01:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定