我有几行C代码测试模运算符如下:
// line 1
printf("%d\n", 5 % (-3)); => output: 2
// line 2
printf("%d\n", -5 % 3); => output: -2
Run Code Online (Sandbox Code Playgroud)
我知道模数的符号取决于分子的符号,但我很好奇为什么不这样做呢?
给定一个数字,在尾随0. 9之前找到5位数!= 362880所以f(9)= 36288 10!= 3628800所以f(10)= 36288 20!= 2432902008176640000所以f(20)= 17664查找f(1,000,000,000,000)
为此,我计算了f(10^6)然后f(10^12) =
(f(10^6))^(10^6) 计算f(n)...我通过删除任何5和相应的2来计算阶乘,以便删除所有尾随零.
但我得到了错误的答案.
方法有问题还是有些愚蠢的错误?
代码供参考
long long po(long long n, long long m, long long mod) {
if (m == 0) return 1;
if (m == 1) return n % mod;
long long r = po(n, m / 2, mod) % mod;
if (m % 2 == 0) return (r * r) % mod;
return (((r * r) % mod) * n) % …Run Code Online (Sandbox Code Playgroud) 我要实现一个向左和向右执行循环旋转的函数.所以我为这两个操作写了同样的东西.例如,如果你正在旋转左边1010变成0101.这是对的吗?
unsigned char rotl(unsigned char c) {
int w;
unsigned char s = c;
for (w = 7; w >= 0; w--) {
int b = (int)getBit(c, w);//
if (b == 0) {
s = clearBit(s, 7 - w);
} else if (b == 1) {
s = setBit(s, 7 - w);
}
}
return s;
}
unsigned char getBit(unsigned char c, int n) {
return c = (c & (1 << n)) >> n;
}
unsigned char setBit(unsigned char …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试用 C 语言为我的程序实现哈希函数。我找到了许多可能的解决方案,但我不理解它们。下面是哈希函数:
int hash(const char *word) {
int hash = 0;
int n;
for (int i = 0; word[i] != '\0'; i++) {
// alphabet case
if (isalpha(word[i]))
n = word[i] - 'a' + 1;
else // comma case
n = 27;
hash = ((hash << 3) + n) % SIZE;
}
return hash;
}
Run Code Online (Sandbox Code Playgroud)
我们为什么要从'a'+1中减去word[i]?另外,我们为什么要做以下事情:hash = ((hash << 3) + n) % SIZE?
我目前正在寻找一个非常快的整数平方根近似值,其中 floor(sqrt(x)) <= veryFastIntegerSquareRoot(x) <= x
平方根例程用于计算素数,如果仅sqrt(x)检查小于或等于 的值是否为 的除数,则计算速度会快得多x。
我目前拥有的是来自 Wikipedia 的这个函数,稍微调整了一下以使用 64 位整数。
因为我没有其他函数可以比较(或者更准确地说,该函数对于我的目的来说太精确了,而且它可能需要更多的时间,而不是高于实际结果。)
我尝试使用C 实现一些纯通用的算法.我坚持使用3向快速排序但不知何故实现不能提供正确的输出.输出几乎排序,但有些键不在应有的位置.代码如下.提前致谢.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static void swap(void *x, void *y, size_t size) {
void *tmp = malloc(size);
memcpy(tmp, x, size);
memcpy(x, y, size);
memcpy(y, tmp, size);
free(tmp);
}
static int cmpDouble(const void *i, const void *j) {
if (*(double *)i < *(double *)j)
return 1;
else if (*(double *)i == *(double *)j)
return 0;
else
return -1;
}
void qsort3way(void *base, int lo, int hi, size_t size,
int (*cmp)(const void *, const void …Run Code Online (Sandbox Code Playgroud) 我在玩C; 看一下这个:
#include <stdio.h>
#include <stdlib.h>
void main() {
printf("%d\n", 1.5);
printf("%f", 0);
}
Run Code Online (Sandbox Code Playgroud)
我期待输出:
0
0.000000
Run Code Online (Sandbox Code Playgroud)
但它打印:
0
1.500000
Run Code Online (Sandbox Code Playgroud)
第一次printf()通过1.5第二次printf()吗?
PS:我知道(%d对于整数,%f浮标).正如我所提到的,我只是在搞乱代码.
PS2:我正在使用DevC++和Code :: Blocks.
鉴于这是合法的
uint8_t bytes[4] = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
这不是:
uint8_t bytes2[4];
bytes2 = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
什么{ 1, 2, 3, 4 }代表?
假设它既不是右值也不是左值.一个预处理器代码糖果扩展到什么?
#include <stdio.h>
int main(void) {
int a[4][2] = { {11, 12}, {21, 22}, {31, 32}, {41, 42} };
int *p = a[0];
printf("%d\n", *p);
printf("%d\n", p);
printf("%d\n", a[0]);
printf("%d\n", &p);
printf("%d\n", &a[0]);
printf("%d\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看看上面的代码.
在我的理解中,因为p等于a[0],&p并且&a[0]应该具有相同的值.
但实际输出如下:
11
1772204208
1772204208
1772204200
1772204208
1772204208
Run Code Online (Sandbox Code Playgroud)
为什么&p和&a[0]不同?
什么是&p代表?
I'm trying to port a knn (k nearest neighbor search ) on a kd-tree that I wrote in Java to C.
The Java output, as expected:
Nearest to Key: 6.0,5.0,4.0
Key:6.0,5.0,4.0,min distance:0.0
Key:5.0,4.0,3.0,min distance:3.0
Key:7.0,6.0,5.0,min distance:3.0
Key:4.0,3.0,2.0,min distance:12.0
Key:3.0,2.0,1.0,min distance:27.0
Run Code Online (Sandbox Code Playgroud)
Java code, class (Its a quick implementation just to get the algorithm working before I start my port):
Nearest to Key: 6.0,5.0,4.0
Key:6.0,5.0,4.0,min distance:0.0
Key:5.0,4.0,3.0,min distance:3.0
Key:7.0,6.0,5.0,min distance:3.0
Key:4.0,3.0,2.0,min distance:12.0
Key:3.0,2.0,1.0,min distance:27.0
Run Code Online (Sandbox Code Playgroud)
Java knn method:
class kd_tree {
public int …Run Code Online (Sandbox Code Playgroud)