有两个有序数组,float a[]={1.5,2,4,4.1,7},b[]={-1.5,1,2.3,3.9,7.0};求两个数组元素彼此之间的绝对值中最小的一个数,即数组的距离。
#include<stdio.h>
#include<math.h>
//法一
void mindist(float a[],int n,float b[],int m)
{
int a_index=0,b_index=0;
float min=10000;
float temp;
while(a_index<n && b_index<m)
{
while(b_index<m)
{
if((temp=fabs(a[a_index]-b[b_index]))<min)
{
min=temp;
b_index++;
}
else
break;
}
a_index++;
}
printf("the mindist is %.2f\n",min);
}
//法二:
int min_distance(int x[],int n, int y[], int m)
{
int minimum = INT_MAX; /* INT_MAX is from limits.h */
int index_x = 0, index_y = 0;
while (index_x < n && index_y < m)
if (x[index_x] >= y[index_y]) {
minimum = min(minimum, x[index_x]-y[index_y]);
index_y++;
}
else {
minimum = min(minimum, y[index_y]-x[index_x]);
index_x++;

该博客探讨如何在两个有序浮点数数组中找到元素间的最小绝对差值。通过比较数组元素,找到了a数组{1.5, 2, 4, 4.1, 7}和b数组{-1.5, 1, 2.3, 3.9, 7.0}之间的最小差值,这是一个涉及C语言和计算最短距离的问题。"
131891533,569322,2023年5月Scratch编程三级考级真题解析:判断题详解,"['Scratch编程', 'Scratch考级', '图形化编程', '编程考试', '编程教育']
最低0.47元/天 解锁文章
1506

被折叠的 条评论
为什么被折叠?



