我刚看到这个代码片段Q4,并想知道我是否理解正确.
#include <stdio.h>
int main(void)
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int*)(&a + 1);
printf("%d %d\n", *(a + 1), *(ptr - 1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的解释:
int a[5] = { 1, 2, 3, 4, 5 };=> a指向数组的第一个元素.换句话说:a包含数组第一个元素的地址.
int *ptr = (int*)(&a + 1);=>这里&a将是一个双指针并指向整个数组.我把它想象成这样:int b[1][5] = {1, 2, 3, 4, 5};这里b指向一个2D数组的行.&a + 1应该指向内存中的下一个整数数组(不存在)[有点像,b + 1指向具有1行的2D数组的第二个(不存在的)行].我们把它转换为int …
我正在尝试使用均匀圆形LBP(1个单位半径邻域中的8个点)来实现基本的人脸识别系统.我正在拍摄一张图像,将其重新调整为200 x 200像素,然后将图像分成8x8小图像.然后,我计算每个小图像的直方图,并获得直方图列表.为了比较2个图像,我计算相应直方图之间的卡方距离并生成分数.
这是我的Uniform LBP实现:
import numpy as np
import math
uniform = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 58, 6: 5, 7: 6, 8: 7, 9: 58, 10: 58, 11: 58, 12: 8, 13: 58, 14: 9, 15: 10, 16: 11, 17: 58, 18: 58, 19: 58, 20: 58, 21: 58, 22: 58, 23: 58, 24: 12, 25: 58, 26: 58, 27: 58, 28: 13, 29: …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "ff.txt"
int main() {
char x[10],y[10];
FILE *fp;
fp = fopen(FILE_NAME, "r+");
if (fp == NULL) {
printf("couldn't find %s\n ",FILE_NAME);
exit(EXIT_FAILURE);
}
fprintf(fp,"Hello2 World\n");
fflush(fp);
fscanf(fp,"%s %s",x,y);
printf("%s %s",x,y);
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我想要做的简化版本.此代码不会在控制台中打印任何内容.如果我删除了fprintf调用,它会打印文件中的前两个字符串,对我而言Hello2 World.为什么会这样?即使在我fflush的fp?
#include "stdio.h"
int main()
{
int n;
printf("Enter n:\n");
scanf("%d",&n);
int arr[n][n];
arr[3][3] = 4;
printf("%d",arr[3][3]);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有使用int arr[n],其中n是变量,在C中是非法的?我想知道这里发生了什么.显然,代码适用于我clang LLVM compiler和IDEOne以及Codeblocks.我想编译器只是通过自动内存分配让事情变得简单.但另一个惊人的事实是,当我尝试设置n为1,2或3时,它仍然有效.
我正在读一本书:[xchg rax,rax].以下是本书的0x03片段,我无法理解这一点.
sub rdx,rax
sbb rcx,rcx
and rcx,rdx
add rax,rcx
Run Code Online (Sandbox Code Playgroud)
我一直在工作一周以来(每天几分钟).我研究了一些试图解决它的事情:数字的有符号表示,减法如何工作,减法后CF的作用.根据这个答案和这里给出的清单.除了可能溢出的情况之外,我没有看到减法后检查CF的重点.
在什么情况下检查进位标志在减法后有用?
我有一个'aabbababacccssdd'我想要生成的字符串['aa', 'bb', 'a', 'b', 'a', 'b', 'a', 'ccc', 'ss', 'dd']
这是我目前的解决方案:
def get_pats(n):
n = str(n) # to support integers
a = len(n)
p = []
pat_start = 0
prev = 0
for b in range(0, a):
if n[b] != n[prev]:
p.append(n[pat_start:b])
prev = b
pat_start = b
p.append(n[pat_start:b+1])
return p
Run Code Online (Sandbox Code Playgroud)
解决方案工作得很好,但我想知道是否有更优雅/ pythonic的方式来做到这一点?
我试图用Python 编写问题12(Project Euler)的解决方案.解决方案太慢了,所以我尝试在互联网上检查其他人的解决方案.我发现这个用C++编写的代码与我的python代码几乎完全相同,只有几个微不足道的差异.
蟒蛇:
def find_number_of_divisiors(n):
if n == 1:
return 1
div = 2 # 1 and the number itself
for i in range(2, n/2 + 1):
if (n % i) == 0:
div += 1
return div
def tri_nums():
n = 1
t = 1
while 1:
yield t
n += 1
t += n
t = tri_nums()
m = 0
for n in t:
d = find_number_of_divisiors(n)
if m < d:
print n, …Run Code Online (Sandbox Code Playgroud) 我试图将数据结构中的一组元素替换为其他值.在python的情况下,在字符串中进行这种替换似乎比在列表中快得多(如下面的基准测试所示).有人可以解释原因.
注意:这些测试是使用python 2.7执行的.
def string_replace_test(s, chars):
"""Replaces a set of chars to 0"""
new = s
for c in chars:
new = new.replace(c, '0')
return new
def list_replace_test(s, chars):
"""Replaces a set of chars to 0"""
for a in xrange(len(s)):
if s[a] in chars:
s[a] = '0'
if __name__ == '__main__':
import timeit
s = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
etfringilla purus. Pellentesque bibendum urna at neque consectetur
at tincidunt nulla luctus. Pellentesque augue lacus, …Run Code Online (Sandbox Code Playgroud)