gpt4 book ai didi

c - C 中的 Dijkstra 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:16:42 29 4
gpt4 key购买 nike

在这里寻求帮助。我必须为 Djikstra 算法编写一个程序。不必从用户那里获取输入或任何东西,只需硬编码。这是我第一次用 C 编写任何代码。并不是那么好用。我得到了我的代码,它在我的脑海中逻辑上是如何工作的。我的问题是,当我运行它时,我什么也得不到。什么都没有打印出来。有人可以帮助我和我一起解决这个问题,那就太好了。我会在尝试查找问题时及时通知您最新情况,但 C 语言比我聪明一点的人可能更容易解决问题。

#include <stdio.h>
void main (){
int ab = 3;//path from a to b
int ac = 7;//path from a to c
int ad = 9;//path from a to d
int bc = 2;//path from b to c
int bd = 4;//path from b to d
int cd = 1;//path from c to d
int a = 10;//number values for position
int b = 20;
int c = 30;
int d = 40;

int position = 10;//starting position a
int currenttravel = 0;
//starting at a
//if (position == 10){
int checker = 40;//check for when at d
do
{
//check for if at a
if (position == 10){
//if path a to b is shortest
if (ab < ac && ab < ad){
position = b;//go to b
printf("%d", &position);
currenttravel+=ab;
}
//or if path a to c is shortest
else if (ac < ad){
position = c;//go to c
printf("%d", &position);
currenttravel+=ac;
}
else{
position = d;
printf("%d", &position);
currenttravel+=ad;
}

}
if (position == 20)//at b
{
if (bc < bd){
position = c;
printf("%d", &position);
currenttravel+=bc;
}
else{
position = d;
printf("%d", &position);
currenttravel+=bd;
}
}
if (position == 30){
position = d;
printf("%d", &position);
currenttravel+=cd;
}
}
while(position != checker);
// }//end if start position is a
printf("%d", currenttravel);
return; //leave function

}

我已尽我所能发表评论,所以希望可以遵循我的逻辑。我可能把它复杂化了,但这应该是一种可行的方法。

有效的固定代码!

#include <stdio.h>
int main (){
int ab = 3;//path from a to b
int ac = 7;//path from a to c
int ad = 9;//path from a to d
int bc = 2;//path from b to c
int bd = 4;//path from b to d
int cd = 1;//path from c to d
int a = 10;//number values for position
int b = 20;
int c = 30;
int d = 40;

int position = 10;//starting position a
int currenttravel = 0;
//starting at a
//if (position == 10){
int checker = 40;//check for when at d
do
{
printf("starting at a \n");
//check for if at a
if (position == a){
//if path a to b is shortest
if (ab < ac && ab < ad){
position = b;//go to b
printf("b \n");
currenttravel+=ab;
}
//or if path a to c is shortest
else if (ac < ad){
position = c;//go to c
printf("c \n");
currenttravel+=ac;
}
else{
position = d;
printf("d \n");
currenttravel+=ad;
}

}
if (position == b)//at b
{
if (bc < bd){
position = c;
printf("c \n");
currenttravel+=bc;
}
else{
position = d;
printf("d \n");
currenttravel+=bd;
}
}
if (position == c){
position = d;
printf("d \n");
currenttravel+=cd;
}
}
while(position != checker);
// }//end if start position is a
printf("%d", currenttravel);
// return; //leave function

}

谢谢大家的帮助。现在我只需要将它转换为 Prim 的算法(这将非常简单,因为我只是不把所有东西都加起来)。也可以尝试不同的起始位置,但现在这可能就足够了。

最佳答案

这是我用 gcc 编译时得到的输出:

main.cpp:2:12: error: ‘::main’ must return ‘int’
void main (){
^

main.cpp: In function ‘int main()’:
main.cpp:64:2: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
return;
^

main.cpp: In function ‘int main()’:
main.cpp:26:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:32:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:37:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:46:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:51:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:57:39: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);

所以首先,它应该是 int mainprintf("%d", position) 到处都是,你也应该删除 return;。该函数应返回一个 int

然后代码执行并打印一些东西:

2030406

你可能想要在中间换行,使用 printf("%d\n", position)。然后:

20
30
40
6

虽然我还没有检查输出的正确性。

关于c - C 中的 Dijkstra 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421581/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com