Trie实现的以下代码在调用函数insert时抛出浮点异常.for循环内部的行检查现有节点是问题所在.
struct Node {
char c;
bool isend;
unordered_map<int, struct Node*> map;
};
void insert(struct Node* root, string contact) {
int size = contact.size();
char ch;
for (int i = 0; i < size; i++) {
ch = contact[i];
// this check is creating problem
if (root->map.find(ch) == root->map.end()) {
struct Node* tmp = (struct Node*) malloc(sizeof(struct Node));
tmp->c = ch;
if (i == (size - 1)) {
tmp->isend = true;
} else {
tmp->isend = false;
}
root->map.insert(make_pair(ch, tmp)); …Run Code Online (Sandbox Code Playgroud) 我想测试我是否理解回溯,所以我尝试了骑士问题。但是我的代码似乎不起作用。它似乎做了一个无限循环,所以也许我对路径的跟踪没有很好地执行。所以我想知道我对这个问题的理解有什么遗漏。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 8
int board[8][8]= {
-1,-1,-1,-1,-1,-1,-1,-1, //1
-1,-1,-1,-1,-1,-1,-1,-1, //2
-1,-1,-1,-1,-1,-1,-1,-1, //3
-1,-1,-1,-1,-1,-1,-1,-1, //4
-1,-1,-1,-1,-1,-1,-1,-1, //5
-1,-1,-1,-1,-1,-1,-1,-1, //6
-1,-1,-1,-1,-1,-1,-1,-1, //7
-1,-1,-1,-1,-1,-1,-1,-1, //8
};
bool isSafe(int x, int y)
{
return ( x >= 0 && x < N && y >= 0 &&
y < N && board[x][y] == -1);
}
int SolveKnight_From_One_Point (int x,int y , int number_Moov) {
if (number_Moov == N*N)
return 1;
if (isSafe(x,y)){
board[x][y] = number_Moov;
if (SolveKnight_From_One_Point(x-2,y+1,number_Moov+1)==1) …Run Code Online (Sandbox Code Playgroud) 我一直在玩一些教程,并且到达了使用“ while”的位置,OR运算符仅在第一个条件为true时停止,而在第二个条件为true时才停止,即使第二个条件已经满足在某些循环中确实如此。
while (humanCount > 0 || skeletonCount > 0)
{
cout << "Humans left: " << humanCount << " | Skeletons left: " << skeletonCount << "\n";
if (currentTurn == 0) // Human Attack
{
if (rollChance(randomNum) >= 0.5f)
{
skeletonCurrentHealth -= rollDamage(randomNum);
if (skeletonCurrentHealth <= 0)
{
skeletonCount--;
skeletonCurrentHealth = skeletonMaxHealth;
}
}
currentTurn = 1;
}
else // Skeleton Attack
{
if (rollChance(randomNum) >= 0.7f)
{
humanCurrentHealth -= rollDamage(randomNum);
if (humanCurrentHealth <= 0)
{
humanCount--;
humanCurrentHealth = …Run Code Online (Sandbox Code Playgroud) I try to learn embedded c for a stm32-microcontorller. I try to wirte a easy blink program, where i use the sleep()-function.
code:
/* Includes ------------------------------------------------------------------*/
#include <unistd.h>
#include "main.h"
int main(void)
{
HAL_Init();
while (1)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
sleep(1); // this line throws a error, when compiling
}
}
Run Code Online (Sandbox Code Playgroud)
the compiler gives me following error:
/usr/lib/gcc/arm-none-eabi/7.4.0/../../../../arm-none-eabi/bin/ld: CMakeFiles/untitled2.elf.dir/Src/main.c.obj: in function `main':
/home/heinrich/CLionProjects/untitled2/Src/main.c:106: undefined reference to `sleep'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/untitled2.elf.dir/build.make:391: untitled2.elf] Fehler 1
make[2]: *** …Run Code Online (Sandbox Code Playgroud) 我创建了一个整数my_int_min并分配了值INT_MIN,
my_int_min = -my_int_min = INT_MIN = -INT_MIN = -2147483648
Run Code Online (Sandbox Code Playgroud)
应该是有效的。所以,我预测表达式的!(-my_int_min & INT_MIN)值应该是0,但实际运行结果是1。
我发现!(-my_int_min&my_int_min)等于0并且(-my_int_min&INT_MIN)等于-2147483648。为什么!(-my_int_min & INT_MIN)不等于0?代码如下。
#include <stdio.h>
#include <limits.h>
int main()
{
int my_int_min = INT_MIN;
printf("INT_MIN=%d\t my_int_min=%d\t -INT_MIN=%d\t -my_int_min=%d\n", INT_MIN, my_int_min, -INT_MIN, -my_int_min);
printf("!(-INT_MIN&INT_MIN)=%d\t !(-my_int_min&INT_MIN)=%d\n", !(-INT_MIN&INT_MIN), !(-my_int_min&INT_MIN));
printf("!(-INT_MIN&my_int_min)=%d\t !(-my_int_min&my_int_min)=%d\n", !(-INT_MIN&my_int_min), !(-my_int_min&my_int_min));
printf("(-my_int_min&INT_MIN)=%d\t !(-my_int_min&INT_MIN)=%d\n", (-my_int_min&INT_MIN), !(-my_int_min&INT_MIN));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
INT_MIN=-2147483648 my_int_min=-2147483648 -INT_MIN=-2147483648 -my_int_min=-2147483648
!(-INT_MIN&INT_MIN)=0 !(-my_int_min&INT_MIN)=1
!(-INT_MIN&my_int_min)=0 !(-my_int_min&my_int_min)=0
(-my_int_min&INT_MIN)=-2147483648 !(-my_int_min&INT_MIN)=1
Run Code Online (Sandbox Code Playgroud) 我需要从std::vector.
我知道这是可以做到的,myvector.erase(myvector.begin() + index)但这对我来说看起来很难看。
像这样的东西myvector.removeindex(index)会更具可读性。
他们是否在 C++20 或 C++23 中计划类似的事情?
memcpy(buf, buf + (pos - offset), len);
Run Code Online (Sandbox Code Playgroud)
然而,
0<=pos<=strlen(buf), 0<=offset<=strlen(buf)
Run Code Online (Sandbox Code Playgroud)
memcpy()在这种情况下使用是否安全?如果没有,会出现什么问题?请提出最佳做法.
我的练习遇到了问题,我必须解释 C 中指针的运行。
您能向我解释一下char *pp和之间的区别吗?(char*) p
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
int n=260, *p=&n;
printf("n=%d\n", n);
char *pp=(char*)p;
*pp=0;
printf("n=%d\n",n);
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
n=260
n=256
我对我所犯下的错误感到非常抱歉!希望你们能帮助我。
我尝试编写一个小程序来打印所有 3 位数字,并且每个数字都不同。这是我写的:
for(int i = 100; i<= 999; i++)
{
if((i%10) != (i/10%10) != (i/100))
printf("%d ",i);
}
Run Code Online (Sandbox Code Playgroud)
而不是打印:101 102 103...
它打印:100 111 122 133...
为什么会发生这种情况?例如,不应打印数字 100: If 0 != 0 != 1 print。
我们知道 BODMAS 规则或首先根据优先级,划分将首先发生。所以7/2 = 3,因为我们正在除(int)/(int)。然后乘以 8 ,所以3*8将是 24。
但是cout<<是给28。
为什么会发生这种情况?
c ×6
c++ ×5
backtracking ×1
gcc ×1
knights-tour ×1
memcpy ×1
review ×1
sleep ×1
stdvector ×1
stm32 ×1