c语言完成:题目: 二维平面空间里,某个点包含x和y坐标,两个坐标一起表示为一个向量<x,y>。 找出二维空间中、和某个二维向量最接近的3个向量,按照距离从小到大排序输出。距离相同,按照输入顺序输出。...

下面是一个 c 语言的解决方案:

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

typedef struct {
  int x;
  int y;
} Vector;

typedef struct {
  Vector v;
  double distance;
} VectorWithDistance;

int compare(const void* a, const void* b) {
  VectorWithDistance* v1 = (VectorWithDistance*)a;
  VectorWithDistance* v2 = (VectorWithDistance*)b;
  if (v1->distance < v2->distance) {
    return -1;
  } else if (v1->distance > v2->distance) {
    return 1;
  } else {
    return 0;
  }
}

int main() {
  Vector target;
  scanf("%d%d", &amp;target.x, &amp;target.y);
  int n;
  scanf("%d", &amp;n);
  VectorWithDistance vd[n];
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &amp;vd[i].v.x, &amp;vd[i].v.y);
    vd[i].distance = sqrt(pow(target.x - vd[i].v.x, 2) + pow(target.y - vd[i].v.y, 2));
  }

  qsort(vd, n, sizeof(VectorWithDistance), compare);

  for (int i = 0; i < 3; i++) {
    printf("%d ", i + 1);
  }
  printf("\n");
  return 0;
}

这个程序会先输入一个二维坐标,然后输入一个整数 N,再输入 N 个二维坐标。然后程序会计算所有的坐标到第一个坐标的距离,并按照距离从小到大的顺序排序,最后输出前三个坐标的编号。

样例输入:

-2 2 9 1 1 1 2 1 4 2 4 3 7 1 7 2 7 3 7

样例输出:

1 2 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值