当前位置:首页 » 编程语言 » c语言穷举法求最小距离
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言穷举法求最小距离

发布时间: 2023-03-10 06:54:59

Ⅰ 求两点之间最短距离的c语言源代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
printf("输入两点坐标:\n");
double x1,y1,x2,y2,length;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
length = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
printf("%lf\n",length);
system("pause");
return 0;
}

Ⅱ 求c语言最短路径算法

#include <iostream>
using namespace std;

const int maxnum = 100;
const int maxint = 999999;

// 各数组都从下标1开始
int dist[maxnum]; // 表示当前点到源点的最短路径长度
int prev[maxnum]; // 记录当前点的前一个结点
int c[maxnum][maxnum]; // 记录图的两点间路径长度
int n, line; // 图的结点数和路径数

// n -- n nodes
// v -- the source node
// dist[] -- the distance from the ith node to the source node
// prev[] -- the previous node of the ith node
// c[][] -- every two nodes' distance
void Dijkstra(int n, int v, int *dist, int *prev, int c[maxnum][maxnum])
{
bool s[maxnum]; // 判断是否已存入该点到S集合中
for(int i=1; i<=n; ++i)
{
dist[i] = c[v][i];
s[i] = 0; // 初始都未用过该点
if(dist[i] == maxint)
prev[i] = 0;
else
prev[i] = v;
}
dist[v] = 0;
s[v] = 1;

// 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中
// 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度
// 注意是从第二个节点开始,第一个为源点
for(int i=2; i<=n; ++i)
{
int tmp = maxint;
int u = v;
// 找出当前未使用的点j的dist[j]最小值
for(int j=1; j<=n; ++j)
if((!s[j]) && dist[j]<tmp)
{
u = j; // u保存当前邻接点中距离最小的点的号码
tmp = dist[j];
}
s[u] = 1; // 表示u点已存入S集合中

// 更新dist
for(int j=1; j<=n; ++j)
if((!s[j]) && c[u][j]<maxint)
{
int newdist = dist[u] + c[u][j];
if(newdist < dist[j])
{
dist[j] = newdist;
prev[j] = u;
}
}
}
}

// 查找从源点v到终点u的路径,并输出
void searchPath(int *prev,int v, int u)
{
int que[maxnum];
int tot = 1;
que[tot] = u;
tot++;
int tmp = prev[u];
while(tmp != v)
{
que[tot] = tmp;
tot++;
tmp = prev[tmp];
}
que[tot] = v;
for(int i=tot; i>=1; --i)
if(i != 1)
cout << que[i] << " -> ";
else
cout << que[i] << endl;
}

int main()
{
freopen("input.txt", "r", stdin);
// 各数组都从下标1开始

// 输入结点数
cin >> n;
// 输入路径数
cin >> line;
int p, q, len; // 输入p, q两点及其路径长度

// 初始化c[][]为maxint
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
c[i][j] = maxint;

for(int i=1; i<=line; ++i)
{
cin >> p >> q >> len;
if(len < c[p][q]) // 有重边
{
c[p][q] = len; // p指向q
c[q][p] = len; // q指向p,这样表示无向图
}
}

for(int i=1; i<=n; ++i)
dist[i] = maxint;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
printf("%8d", c[i][j]);
printf(" ");
}

Dijkstra(n, 1, dist, prev, c);

// 最短路径长度
cout << "源点到最后一个顶点的最短路径长度: " << dist[n] << endl;

// 路径
cout << "源点到最后一个顶点的路径为: ";
searchPath(prev, 1, n);
}

Ⅲ c语言 如何求一点,使其到平面上其他已知点的距离和最小

能具体点吗?
如果点少,或许可以用数学几何方法求
如果点多,估计就只能循环求近似点了

min=(x,y)到到其他点的距离和 (x,y可随意,一般取x=最小值,y=最小值)
for(x=最小值;x≤最大值;x增加)
for(y=最小值;y≤最大值;y增加)
if(点(x,y)到其他点的距离和 < min)
记录下(x,y);
这种方法只能求一个近似点,精确度取决于你设置的x、y的增加速率

Ⅳ C语言穷举法

为啥它们范围会这样取,为啥x会从1-14,这是需要仔细推算的。因为本题的计算量很小,有时就图自己省力(少算一点)让计算机多算一点。

因为x至少是1,而y>x,z>y,为简单起见,而x、y、z的单价分别为a、b、c,所以,ax+by+cz=800

而ax+by+cz>ax+bx+cx

所以,800>(a+b+c)x

x<800/(30+20+10),即x<=13(取整数)

同样的道理,y最小是2,800=30+20y+10z>30+20y+10y,y最大=770/30=25

Ⅳ 如何用C语言编程解决:空间两线段间的最小距离,已经两线段起点终点坐标。在线求解答

我可以给你思路:
先判断是否相交,相交则距离为0;
若不相交,则最短距离必然某两个端点间的距离,求出线段1起点、终点到线段2起点、终点的距离,挑出4个值中最小的即是。
注:判断是否相交,网上有很多代码,随便google一下
如果你会写c语言代码,我相信你自己能够办到了

Ⅵ C语言最短距离问题

我会。
你的意思是找到两个点。然后是其他点到这个两个点的距离和最小 是吧。

Ⅶ 听到说C语言的穷举法,具体怎么做啊希望大家给我指点下。谢啦

所谓的穷举法就是将所有可能的结果按顺序一个一个来进行判断...是计算量最大一种算法

Ⅷ 如何用C语言计算两点之间距离

用C语言计算两点之间距离的参考代码:

#include <stdio.h>

#include <stdlib.h>

#include "math.h"

typedef struct point {

double x;

double y;

}point;

point array[30001];

double distance(point a,point b);

int main(int argc, char *argv[])

{

while(1){

int n,i,j,count;

double maxdis,temp;

scanf("%d",&n);

for(i = 0;i<n;i++){

scanf("%f %f",&array[i].x,&array[i].y);

}

for(i = 0;i<n;i++){

//printf("%d %d ",array[i].x,array[i].y);

//printf("dis:%.2f ",distance(array[0],array[i]));

}

maxdis = 0.0;

for(i = 0;i<n;i++){

for(j = i;j<n;j++){

if(distance((array[i]),(array[j])) > maxdis ){

//printf("maxdis = :%.2f ",distance((array[i]),(array[j])));

maxdis =distance((array[i]),(array[j]));

}

}

}

printf("%.2f",maxdis);

}

system("pause");

return 0;

}

//计算x y之间距离

double distance(point a,point b){

return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y -b.y)*(a.y -b.y) );

}

(8)c语言穷举法求最小距离扩展阅读:

C语言编写注意事项:

1、所有自定义变量必须声明才能使用。

2、每行只书写一条语句, 在运算符两边加上一个空格, 便于阅读。

3、整数除法将自动舍位, 不会进行四舍五入的操作。

4、for(初始化部分;条件部分;增长部分) - 比while 更适用于初始化和增长步长都是单条语句情况下。

5、使用#define 名字 替换文本 对部分"幻数" 赋予意义便于阅读 #define结尾不需要“;”号结束。