微软面试题,求数组中两两之差绝对值最小的值
答案:1 悬赏:30 手机版
解决时间 2021-04-10 02:55
- 提问者网友:玫瑰园
- 2021-04-09 08:19
微软面试题,求数组中两两之差绝对值最小的值
最佳答案
- 五星知识达人网友:我住北渡口
- 2021-04-09 09:01
#include <stdio.h>
#include <math.h>
int mindiff(int a[],int n) {
int i,j,d,diff = abs(a[0]);
for(i = 0; i < n - 1; ++i) {
for(j = i + 1; j < n; ++j) {
d = abs(a[i] - a[j]);
if(diff > d) diff = d;
if(diff == 0) return 0;
}
}
return diff;
}
int main() {
int a[] = {,1173,,58747,37301,69807,};
int n = sizeof(a)/sizeof(a[0]);
printf("最小差值的绝对值是:%d\n",mindiff(a,n));
return 0;
}
#include <math.h>
int mindiff(int a[],int n) {
int i,j,d,diff = abs(a[0]);
for(i = 0; i < n - 1; ++i) {
for(j = i + 1; j < n; ++j) {
d = abs(a[i] - a[j]);
if(diff > d) diff = d;
if(diff == 0) return 0;
}
}
return diff;
}
int main() {
int a[] = {,1173,,58747,37301,69807,};
int n = sizeof(a)/sizeof(a[0]);
printf("最小差值的绝对值是:%d\n",mindiff(a,n));
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯