小编Sha*_*ars的帖子

瓦利斯的pi公式

def wallis(n):
    pi = 0.0

    for i in range(n):
        left = (2 * i)/(2 * i - 1)
        right = (2 * i)/(2 * i + 1)
        total = left * right
        pi = pi + total

    return pi

print wallis(1000)
print wallis(10000)
print wallis(100000)
Run Code Online (Sandbox Code Playgroud)

我完全复制了公式,但我一直得到0作为输出.有人可以告诉我我做错了什么.Python 2.7.

公式的链接在这里

python python-2.7

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

如何在C中为char**动态分配内存

我将如何在此函数中动态分配内存到char**列表?

基本上这个程序的想法是我必须从文件中的单词列表中读取.我不能假设最大字符串或最大字符串长度.

我必须用C字符串做其他的东西,但那些东西我应该没问题.

谢谢!

void readFileAndReplace(int argc, char** argv)
{
    FILE *myFile;
    char** list;
    char c;
    int wordLine = 0, counter = 0, i;
    int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0;

    myFile = fopen(argv[1], "r");

    if(!myFile)
    {
        printf("No such file or directory\n");
        exit(EXIT_FAILURE);
    }

    while((c = fgetc(myFile)) !=EOF)
    {
        numberOfChars++;
        if(c == '\n')
        {
            if(maxNumberOfChars < numberOfChars)
                maxNumberOfChars += numberOfChars + 1;

            numberOfLines++;
        }
    }

    list = malloc(sizeof(char*)*numberOfLines);

    for(i = 0; i < wordLine ; i++)
        list[i] …
Run Code Online (Sandbox Code Playgroud)

c arrays c-strings dynamic-allocation

2
推荐指数
1
解决办法
7717
查看次数

很难理解汇编语言

假设以下值存储在指定的存储器地址和寄存器中:

Address    Value            Register     Value
0x100      0xFF             %eax         0x100
0x104      0xAB             %ecx         0x1
0x108      0x13             %edx         0x3
0x10C      0x11


Fill in the following table showing the values for the indicated operands:

Operand           Value    //Solutions at the end of the chapter
%eax              _____    //0x100
0x104             _____    //0xAB
$0x108            _____    //0x108
(%eax)            _____    //0xFF
4(%eax)           _____    //0xAB
9(%eax, %edx)     _____    //0x11
260(%ecx, %edx)   _____    //0x13
0xFC(,%ecx,4)     _____    //0xFF
(%eax, %edx,4)    _____    //0x11
Run Code Online (Sandbox Code Playgroud)

有人可以用外行的话向我解释如何做到这一点.这不是hmwk(在某些阅读中有练习问题,在本章末尾有答案),我只是不理解阅读.

syntax x86 assembly

2
推荐指数
2
解决办法
3664
查看次数

删除整个链表C.

我的程序使用链接列表按顺序输入数字.

My input: 2 4 23 34 534 543 
Run Code Online (Sandbox Code Playgroud)

当我去删除列表时,我得到了这个:

137429056 137428992 137429040 137429024 137429008 0 
Run Code Online (Sandbox Code Playgroud)

这是为什么?

void deleteList(node* head)
{
    if(head == NULL)
        printf("List is empty\n");

    else
        deleteList(head->next);

    free(head);
}
Run Code Online (Sandbox Code Playgroud)

c recursion linked-list

2
推荐指数
1
解决办法
3496
查看次数

C中链接列表的动态数组

基本上我必须在链表中存储单词,每个字符都有自己的节点.我对嵌套结构感到困惑.我如何进入下一个节点?我知道我这样做完全错了,这就是我要问的原因.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
    char letter;
}NODE;


typedef struct handle
{
    NODE **arr;
}HANDLE;

HANDLE * create();


void insert(handle **, char, int);

int main(int argc, char **argv)
{
    FILE *myFile;
    HANDLE *table = (HANDLE *)malloc(sizeof(HANDLE));
    NODE *linked = (NODE *)malloc(sizeof(NODE));
    int counter = 0;

    linked = NULL;
    table->arr[0] = linked;

    char c;


    myFile = fopen(argv[argc], "r");

    while((c = fgetc(myFile)) != EOF)
    { 
        if(c != '\n')
            insert(&table, c, counter);

        else
        {
            counter++;
            continue;
        } …
Run Code Online (Sandbox Code Playgroud)

c linked-list dynamic-arrays

2
推荐指数
1
解决办法
2534
查看次数

获取从节点到自身的最短路径的算法——邻接矩阵——Java

有向图

有向图

public int dijkstra(){
    boolean[] visited = new boolean[gSize];

    int src = 1;
    int dest = 1;
    int[] distance = new int[5];
    int[] part = new int[5];
    int min;
    int nextNode = 0;

    for(int i = 0; i < 5; i++)
    {
        visited[i] = false;
        part[i] = 0;

        for(int j = 0; j < 5; j++)
            if(arr[i][j] == -1)
                arr[i][j] = 999; //gives it a high value to ignore
    }

    distance = arr[src];
    distance[src] = 0;

    visited[src] = true;

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

java algorithm directed-graph dijkstra adjacency-matrix

2
推荐指数
1
解决办法
3200
查看次数

如何在 SQL Server 中切换服务器连接?

我有一个事务插入到两个不同服务器上的两个不同表中。我想做一些插入,然后切换到另一台服务器并进行更多插入。我如何通过 SQL 实现这一点?

如果这是重复的,我深表歉意,但我无法通过 StackOverflow 或 Google 找到我的答案。谢谢!

我正在使用 SQL Server 2016 和 Management Studio

sql t-sql sql-server transactions

2
推荐指数
1
解决办法
4403
查看次数

ATT组装(算术和逻辑运算)

// Code I'll be working with
int shift_left2_rightn(int x, int n)
{
    x <<= 2;
    x >>= n;
    return x;
}
Run Code Online (Sandbox Code Playgroud)

问题1.左移= SALSHL.我的书说他们有同样的效果.那为什么有两个班次操作?

例如:

movl    8(%ebp), %eax    //Get x
_______                  //x <<= 2 
Run Code Online (Sandbox Code Playgroud)

我的书给出了答案

sall    $2, %eax
Run Code Online (Sandbox Code Playgroud)

shll    $2, %eax 
Run Code Online (Sandbox Code Playgroud)

也是正确的答案?

问题2:

通俗地说,SHR和之间有什么区别SAR?我的书说一个是逻辑移位(用零填充),另一个是算术移位(填充符号位的副本).

用0 /符号位填充什么?

例如:

我怎么知道哪一个使用以下装配说明?

movl   12(%ebp), %ecx   //Get n
______                  //x >>=n
Run Code Online (Sandbox Code Playgroud)

这本书的背面有答案

sarl   %cl, %eax 
Run Code Online (Sandbox Code Playgroud)

请向我解释如果我们使用会发生什么shrl.

感谢您的帮助,了解这一点!

x86 assembly bit-shift att

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

我的Python代码输出的说明

基本上它几乎适用于除了0.93之外我尝试过的几乎所有情况.然后我在while循环中添加了"print money"以查看每次循环后它正在做什么,这就是发生的事情:

Enter an amount less than a dollar: 0.93
0.68
0.43
0.18
0.08
0.03
0.02
0.01
3.81639164715e-17
-0.01
Your change is 3 quarters 1 dimes 1 nickels 4 pennies
Run Code Online (Sandbox Code Playgroud)

有人能解释一下到底是怎么回事吗?

money = input("Enter an amount less than a dollar: ")
quarter = 0
dime = 0
nickel = 0
penny = 0

while money > 0.00:
    if money >= 0.25:
        quarter = quarter + 1
        money = money - 0.25

    elif money >= 0.10:
        dime = dime+1
        money = …
Run Code Online (Sandbox Code Playgroud)

python floating-point

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

Kong API 网关路由的目的是什么

我试图更好地理解 Kong API Gateway - Routes

假设我向 Kong Admin API 添加了一个服务。该服务只允许 GET 请求,当您向该服务发出 GET 请求时,它所做的只是生成一个随机数。

我已经向管理 API 发布了一个新服务。为什么我需要路线?我看过文档,但我仍然不明白它的目的。

routes kong

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