小编vuo*_*len的帖子

C++打印速度比C快得多

我正在网上参加一个小型的编程竞赛.基本上我所做的就是解决任务,编写算法并发送我的代码,由竞争对手的服务器自动评估.

服务器接受各种编程语言.所有任务基本上都要求程序从终端获取输入并输出正确的终端.所以在竞争对手的网站上,我注意到他们支持的语言之一是C++,他们使用g ++来编译它.好吧,因为我不是那么流利的C++,而不是CI的想法,我会用C回答我的答案.

这对第一项任务非常有用.但是在第二个任务中,我经常达到程序执行时间的限制(2秒)

这是我的C代码:

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>

uint8_t get_bit(uint64_t k) {
    ...
}

int main(int argc, char *argv[]) {
    uint64_t n;
    uint64_t k;
    scanf("%u", &n);

    uint64_t i;
    for (i = 0; i < n; i++) {
        scanf("%u", &k);
        printf("%d\n", get_bit(k));
    }

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

所以我的算法定义在get_bit.服务器在我的程序上运行3个不同的测试,具有不同的值,大多数增加以使程序运行更长时间.

但是,由于运行时间超过2秒,因此C中的此代码未通过测试.几个小时尝试不同的解决方案无济于事,我终于尝试使用一些不同的打印方法将我的代码作为C++提交.

这是我的C++主程序(程序的其余部分基本保持不变):

int main(int argc, char *argv[]) {
    uint64_t n;
    uint64_t k;
    cin >> n;

    uint64_t i;
    for (i = 0; i < n; i++) …
Run Code Online (Sandbox Code Playgroud)

c c++ printf cout

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

奇怪的"未定义引用"错误

所以我试图在使用C的Windows API上创建一个非常简单的抽象库.我制作了一个简单的Makefile,当我尝试使用Msys制作项目时,我得到了这个:

winapi.o: In function `get_client_pid':
(long path)/winapi.c:13: undefined reference to `GetExtendedTcpTable'
(long path)/winapi.c:31: undefined reference to `GetExtendedTcpTable'
collect2.exe: error: ld returned 1 exit status
make: *** [lib] Error 1
Run Code Online (Sandbox Code Playgroud)

和get_client_pid()在winapi.c中定义为

#include <stdint.h>
#include <stdio.h>
#include <windows.h>
#include <Iphlpapi.h>

#define true 1
#define false 0


    uint32_t get_client_pid()
    {
        MIB_TCPTABLE_OWNER_PID *tcpTable;
        PDWORD tableSize;
        DWORD ret = GetExtendedTcpTable(tcpTable, 
                                        tableSize, 
                                        true, 
                                        2, 
                                        TCP_TABLE_OWNER_PID_ALL,
                                        0);

        if (ret == NO_ERROR) {
            printf("ERROR get_client_pid: No error with table size at 0?\n");
            return 0;
        }
        if (ret …
Run Code Online (Sandbox Code Playgroud)

c makefile linker-errors

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

在Python中反转代数表达式字符串的算法

是否有一种简单的方法来使函数反转算法,例如:

>>> value = inverse("y = 2*x+3")
>>> print(value)
"x = (y-3)/2"
Run Code Online (Sandbox Code Playgroud)

如果您无法为该功能制作实际代码,请向我推荐使这项任务更容易的工具.该函数仅用于与+, - ,*和/的逆算法.

python expression function symbolic-math sympy

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

标签 统计

c ×2

c++ ×1

cout ×1

expression ×1

function ×1

linker-errors ×1

makefile ×1

printf ×1

python ×1

symbolic-math ×1

sympy ×1