גלע*_*רקן -1 javascript c
由于我不熟悉C,我在C中尝试了第二个矩阵转置示例,简单地转换为JavaScript(下面的代码).它冻结了浏览器.
有人可以帮我理解可能导致问题的原因吗?C程序在ideone中运行良好.
正如Nirk所指出的那样,C程序中的除法是整数而不是浮点,因此循环不会在没有使用的情况下终止Math.floor.
C代码:
#include <stdio.h>
void transpose(double *m, int w, int h)
{
int start, next, i;
double tmp;
for (start = 0; start <= w * h - 1; start++) {
next = start;
i = 0;
do { i++;
next = (next % h) * w + next / h;
} while (next > start);
if (next < start || i == 1) continue;
tmp = m[next = start];
do {
i = (next % h) * w + next / h;
m[next] = (i == start) ? tmp : m[i];
next = i;
} while (next > start);
}
}
Run Code Online (Sandbox Code Playgroud)
JavaScript代码:
function transpose(m, w, h)
{
var start, next, i,
tmp
for (start = 0; start <= w * h - 1; start++) {
next = start
i = 0
do { i++
next = (next % h) * w + next / h
} while (next > start)
if (next < start || i == 1) continue
tmp = m[next = start]
do {
i = (next % h) * w + next / h
m[next] = (i == start) ? tmp : m[i]
next = i
} while (next > start)
}
}
function main()
{
var j
var m = []
for (j = 0; j < 15; j++) m[j] = j + 1
console.log("before transpose:")
console.log(m)
transpose(m, 3, 5)
console.log("\nafter transpose:")
console.log(m)
}
main()
Run Code Online (Sandbox Code Playgroud)