wait(null)和wait(&status)c系统编程有什么区别?
指针状态的内容是什么?
在此链接指定的编码竞赛中,您需要读取大量数据stdin,进行一些计算并提供大量数据stdout.
在我的基准测试中,尽管我尽可能地尝试优化它,但几乎只有I/O才需要时间.
你输入的是一个字符串(1 <= len <= 100'000)和q行的一对int,其中q也是1 <= q <= 100'000.
我在100倍大的数据集(len = 10M,q = 10M)上对我的代码进行基准测试,这就是结果:
Activity time accumulated
Read text: 0.004 0.004
Read numbers: 0.146 0.150
Parse numbers: 0.200 0.350
Calc answers: 0.001 0.351
Format output: 0.037 0.388
Print output: 0.143 0.531
Run Code Online (Sandbox Code Playgroud)
通过实现我自己的格式化和数字解析内联我设法将时间缩短到使用printf和时间的1/3 scanf.
然而,当我将我的解决方案上传到比赛网页时,我的解决方案需要1.88秒(我认为这是超过22个数据集的总时间).当我看到高分时,有几个实现(在c ++中)在0.05秒内完成,比我快了近40倍!怎么可能?
我想我可以通过使用2个线程来加速它,然后我可以开始计算并写入stdout,同时仍然从stdin读取.然而,这将减少min(0.150, 0.143)我的大型数据集的理论上最佳情况的时间.我仍然没有接近高分...
在下图中,您可以看到消耗时间的统计信息.
该程序由网站编译,具有以下选项:
gcc -g -O2 -std=gnu99 -static my_file.c -lm
Run Code Online (Sandbox Code Playgroud)
和定时这样:
time ./a.out < sample.in > sample.out …Run Code Online (Sandbox Code Playgroud) 可能重复:
这是如何工作的?河内解决方案奇怪的塔
在浏览谷歌的过程中,我发现这个有趣的解决方案是Tower Of Hanoi,它甚至不使用堆栈作为数据结构.
任何人都能简单地解释一下,它到底在做什么?
这个解决方案真的可以接受
码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, x;
printf("How many disks?\n");
scanf("%d", &n);
printf("\n");
for (x=1; x < (1 << n); x++)
printf("move from tower %i to tower %i.\n",
(x&x-1)%3, ((x|x-1)+1)%3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新:3号硬编码在这里做什么?
我想知道两个线程是否可以修改同一个数组的元素.
如果我有unsigned char array[4],可以thread1设置array[0]和array[1]'A'和thread2设置array[2]和array[3]'B'同时没有问题?
我们可以使用#ifdef _linux_或者#ifdef __APPLE__来识别用户的操作系统。但是我们可以定义不同版本的操作系统吗?例如 macOS Big Sur 和 macOS Monterey 之间。
在寻找答案时,我遇到了Availability.h库,但我不太明白如何找到正确的操作系统版本
我是Buffer Overflow漏洞的新手,我从一个简单的C程序开始.
码
#include <stdio.h>
#include <strings.h>
void execs(void){
printf("yay!!");
}
void return_input (void)
{
char array[30];
gets(array);
}
int main()
{
return_input();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译阶段
我通过禁用stack protectoras来编译上面的cc程序:
cc test.c -o test -fno-stack-protector
Run Code Online (Sandbox Code Playgroud)
使用elf文件的转储objdump如下:
0804843b <execs>:
804843b: 55 push %ebp
804843c: 89 e5 mov %esp,%ebp
804843e: 83 ec 08 sub $0x8,%esp
8048441: 83 ec 0c sub $0xc,%esp
8048444: 68 10 85 04 08 push $0x8048510
8048449: e8 b2 fe ff ff call 8048300 <printf@plt>
804844e: …Run Code Online (Sandbox Code Playgroud) 我可以比较#definevarible和char *strcmp如下.
#include<stdio.h>
#include<string.h>
#define var "hello"
int main()
{
char *p ="hello";
if(strcmp(p,var)==0)
printf("same\n");
else
printf("not same\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如上例所示#define,是否存在任何风险comapre char *?
由于某些原因,我的回文功能不起作用,我会喜欢它的一些帮助:
码
int Pal(char *s, int a, int b)
{
if (a>= b)
return 1;
if (s[a] != s[b])
return 0;
return Pal(s, ++a , --b);
}
int main()
{
char *s = "civic";
if (Pal(s , 1, strlen(s)))
printf("YES\n");
else
printf("No\n");
}
Run Code Online (Sandbox Code Playgroud)
它继续打印否,我对为什么会发生这种情况一无所知.
有人可以告诉我为什么我SMTPServerDisconnected("Connection unexpectedly closed")从以下代码中收到错误吗?
import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
MY_ADDRESS = '---'
PASSWORD = '---'
def get_contacts(filename):
"""
Return two lists names, emails containing names and email
addresses
read from a file specified by filename.
"""
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
def read_template(filename):
"""
Returns a Template object comprising the contents of the
file …Run Code Online (Sandbox Code Playgroud) 尝试删除文本文件中包含特定模式的行。我有以下代码不起作用
grep -v "$varName" config.txt
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何让它正常工作,我想让它使用grep而不是sed.