我正在研究n-queen回溯.有人可以向我解释如何other_row_pos检查对角线?我不确定它为什么会起作用或者它是如何工作的.
取自wikibooks - http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/N-Queens:
bool isSafe(int queen_number, int row_position) {
// Check each queen before this one
for(int i=0; i<queen_number; i++) {
// Get another queen's row_position
int other_row_pos = position[i];
// Now check if they're in the same row or diagonals
if(other_row_pos == row_position || // Same row
other_row_pos == row_position - (queen_number-i) || // Same diagonal
other_row_pos == row_position + (queen_number-i)) // Same diagonal
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud) 我需要能够更改 Anaconda 查看.condarc文件的位置。它试图访问的驱动器不再存在,这导致几乎每个 conda 命令都失败。我尝试卸载并重新安装 Anaconda。我也试过conda config --file C:\.condarc
这出奇地难以搜索 - 任何帮助将不胜感激!
我正在使用read()in 阅读不同大小的文件(1KB - 1GB)C.但每次我检查page-faults使用时perf-stat,它总是给我相同(几乎)的值.
我的机器:( 虚拟机上的fedora 18,RAM - 1GB,磁盘空间 - 20 GB)
uname -a
Linux localhost.localdomain 3.10.13-101.fc18.x86_64 #1 SMP Fri Sep 27 20:22:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
mount | grep "^/dev"
/dev/mapper/fedora-root on / type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
Run Code Online (Sandbox Code Playgroud)
我的代码:
10 #define BLOCK_SIZE 1024
. . .
19 char text[BLOCK_SIZE];
21 int total_bytes_read=0;
. . .
81 while((bytes_read=read(d_ifp,text,BLOCK_SIZE))>0)
82 {
83 write(d_ofp, text, bytes_read); // writing …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char p[5];
char q[]="Hello";
int i=0;
strcpy(p,"Hello");
printf("strlen(p)=%d\n",strlen(p));
printf("sizeof(p)=%d\n",sizeof(p));
printf("strlen(q)=%d\n",strlen(q));
printf("sizeof(q)=%d\n",sizeof(q));
for(i=0;i<6;i++)
{
printf("p[%d]=%c\tq[%d]=%c\n",i,p[i],i,q[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
strlen(p)=5
sizeof(p)=5
strlen(q)=5
sizeof(q)=6
p[0]=H q[0]=H
p[1]=e q[1]=e
p[2]=l q[2]=l
p[3]=l q[3]=l
p[4]=o q[4]=o
p[5]= q[5]=
Run Code Online (Sandbox Code Playgroud)
我试图了解Thread-local storage (TLS) 类型的实现.可用在C++11如thread_local关键字或在C作为__thread关键字.这篇维基百科文章说:
有时希望引用相同静态或全局变量的两个线程实际上是指不同的存储器位置,从而使得变量线程局部,一个规范的例子是C错误代码变量
errno.
这用于为线程创建static或global变量本地,以便其他线程无法访问它们.
我的问题是这些变量如何存储在内存中,以便它成为线程的本地变量?
在所有这些本质上是全局/静态变量之后,是什么阻止其他线程访问它们?它们是否保留在某些特殊数据段中?
例如,我有一个64位var a和一个8位charb
unsigned long long a = 0x1234567890123456;
unsigned char b = 0x78;
Run Code Online (Sandbox Code Playgroud)
我想a成为0x12345678901234 78
我想要最右边8位a设置b,并保持56位不变,我该怎么办?
a = a & (0xFFffFFffFFffFFff | b); //is this correct?
Run Code Online (Sandbox Code Playgroud)
??
好吧,我正在尝试在 for 循环中运行服务器端和客户端。我第一次运行它时,它运行良好,但第二次连接失败或卡在 accept() 处。这是我的代码:
客户端代码:
for(i=0;i<2;i++)
{
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nError : Connect Failed\n");
return 1;
}
//***************Writing_I***************
memset(recvBuff, '0',sizeof(recvBuff));
ticks = time(NULL);
snprintf(recvBuff, sizeof(recvBuff), "%.24s\r\n", ctime(&ticks));
if((n = send(sockfd, recvBuff, strlen(recvBuff), 0))<0)
{
printf("Error : send operation failed\n");
}
//***************Reading_I***************
memset(recvBuff, '0', sizeof(recvBuff));
n= recv(sockfd, recvBuff, sizeof(recvBuff)-1, 0);
{
printf("bytes read: %d\n", n);
recvBuff[n] = '\0';
printf("Data: %s\n",recvBuff);
}
if(n < 0)
{
printf("\n Read error \n");
}
}
close(sockfd);
Run Code Online (Sandbox Code Playgroud)
服务器代码:
if((connfd …Run Code Online (Sandbox Code Playgroud) 面试官 - 如果您没有工具来检查如何检测内存泄漏问题?
回答 - 我将读取代码并查看我分配的所有内存是否已由代码本身释放.
面试官不满意.有没有其他方法可以这样做?
我有这个代码来读取文件mmap并使用它打印printf.该文件有10行,0-9每行包含nos .
我的问题是:
1.为什么我的代码不会终止EOF?也就是为什么不停下来while (data[i]!=EOF)?
2.当我运行它时while (data[i]!=EOF),程序总是终止于data[10567]?页面大小的位置4096 bytes.请问10567字节有什么意义呢?
编辑:我不是在寻找替代方案,如使用fscanf,fgets.
谢谢!
码:
10 int main(int argc, char *argv[])
11 {
12 FILE *ifp, *ofp;
13 int pagesize, fd, i=0;
14 char *data;
15 struct stat sbuf;
16
18 if ((ifp = fopen("/home/t/workspace/lin", "r"))==NULL)
19 {
20 fprintf(stderr, "Can't open input file\n");
21 exit(1);
22 }
28 fd = …Run Code Online (Sandbox Code Playgroud) 我需要打印用户从命令行输入的单词列表。现在,字母顺序工作正常,但是当我反向打印时,输出顺序不正确。我尝试了很多事情,但现在想法不清了。任何人?这是代码:
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument("user_string", help = 'User string')
argparser.add_argument("--reverse", "-r", action="store_true", dest="reverseflag")
args = argparser.parse_args()
user_string = args.user_string
words_to_sort = user_string.split()
if len(words_to_sort) < 2:
args.user_string
# alerts user to input more than 1 word
print("Invalid command line arguments to program. Please, supply two or more strings to sort.")
if len(words_to_sort) > 1 and (args.reverseflag == True):
words_to_sort = sorted(args.user_string, reverse=True)
print(*words_to_sort)
else:
words_to_sort.sort()
for word in words_to_sort:
print(word)
Run Code Online (Sandbox Code Playgroud)
这是我从命令行得到的:
PS C:\Users\desktop\folder> python mysort.py --reverse "all mall …Run Code Online (Sandbox Code Playgroud)