小编Di *_*ang的帖子

我写了一个JavaScript来在画布上移动一只猫:"ฅ(*ΦωΦ*)ฅ"但猫很奇怪地跳了起来

我创建了一个脚本,它使用HTML输入按钮在画布上移动猫.每次单击都会沿着单击的方向将cat移动10个像素(moveUp(); moveDown(); moveLeft(); moveRight();).这个脚本适用于前10-20次点击,但随后猫最终会跳转或卡在一个位置.

我不知道它为什么会这样.有人可以帮忙吗?

程序在jsfiddle上,你可以测试一下

https://jsfiddle.net/rockmanxdi/h2sk2sjz/2/

JavaScript代码如下:

let surface=document.getElementById("drawingArea");
let ctx=surface.getContext("2d");
let cor_x;
let cor_y;

/** draw a cat 
    *   input the coordinates x and y for the center of the cat
    *   does not return, output the drawing only.
    */
let drawCat = function (x, y) {

        ctx.save();
        ctx.translate(x, y);
        ctx.fillText("?(*???*) ?", -20,-5);
        ctx.restore();

        };

let updateCoordinate = function(x_increment,y_increment){
        console.log("before:" + cor_x + "/" + cor_y);
        cor_x += 10 * x_increment;
        cor_y += 10 * y_increment;
        console.log("updated:" + …
Run Code Online (Sandbox Code Playgroud)

html javascript canvas

6
推荐指数
1
解决办法
513
查看次数

检查2个数组是否具有相同的内容,忽略顺序,没有排序.什么是最有效的算法

乱码的数组示例应返回1:

a = {10,15,20}, b = {10,15,20}
a = {99}, b = {99}
a = {1,2,3,4,5}, b = {5,3,4,2,1}
a = {}, b = {} (i.e. len = 0)
a = {2,1,3,4,5}, b = {1,2,4,3,5} 
Run Code Online (Sandbox Code Playgroud)

乱码的数组示例应返回0:

a = {1,1}, b = {1,2}
a = {10,15,20}, b = {10,15,21}
a = {1,2,3,4,5}, b = {5,3,4,2,2} 
Run Code Online (Sandbox Code Playgroud)

我在C中的代码是这样的,但它是一个O(N ^ 2)效率不高.

int scrambled( unsigned int a[], unsigned int b[], unsigned int len )
{
    int count1 = 0;
    int count2 = 0;

    for (int …
Run Code Online (Sandbox Code Playgroud)

algorithm

3
推荐指数
1
解决办法
243
查看次数

单处理器环境可以防止竞争条件吗?

当多个处理器工作时,这些进程同时工作。当多个线程访问某个公共数据区域时会发生竞争条件,一个可能会覆盖另一个值。

那么,如果是单处理器单核环境,能不能防止竞争条件的发生呢?

帮我澄清这个困惑,谢谢。

operating-system race-condition

3
推荐指数
1
解决办法
1725
查看次数

fork()返回0,但子进程getpid()!= 0.为什么?

这是测试fork()系统调用的C代码:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>

int main(int argc, char *argv[])
{

    printf("I am: %d\n", (int)getpid());

    pid_t pid = fork();

    printf("fork returned: %d\n", (int)pid);

    if (pid < 0)
    {
        perror("fork failed");  
    }

    if (pid==0)
    {
        printf("I am the child with pid %d\n", (int)getpid());
        sleep(5);
        printf("Child exiting...\n");
        exit(0);
    }

    printf("I am the parent with pid %d, waiting for the child\n", (int)getpid());
    wait(NULL);
    printf("Parent ending. \n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

终端输出是:

I am: 25110
fork returned: 25111
I am the parent with pid …
Run Code Online (Sandbox Code Playgroud)

c fork pid process wait

-3
推荐指数
1
解决办法
2437
查看次数