英文:
How to make to make Aitken’s array using C without using array?
问题
以下是翻译好的内容:
Aitken's array(艾特肯数组)是一个由行读取的数字三角形,定义如下:an, k,其中 n ≥ 0,0 ≤ k ≤ n,a0, 0 = 1,an, 0 = an−1, n−1,an, 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 ≤ k ≤ n} 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 <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 ", next);
}
printf("\n");
}
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>0, i=0 ) = f( r-1, r-1 )
f( r>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 < n; ++r ) {
for ( size_t i = 0; i < r; ++i ) {
printf( "%d ", f( r, i ) );
}
printf( "%d\n", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论