小米9.15算法笔试

第一题  动规
第二题  NMS,为什么我用c++写只能ac14%?
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;

bool cmp(vector<float>&a, vector<double>&b) {
return a[4]>=b[4];
}

double calcIOU(vector<double>& bbox1, vector<double>& bbox2) {
double iou = 0.0;
double interX1, interY1, interX2, interY2;
interX1 = max(bbox1[0], bbox2[0]);
interY1 = max(bbox1[1], bbox2[1]);
interX2 = min(bbox1[2], bbox2[2]);
interY2 = min(bbox1[3], bbox2[3]);
double interArea = max(0.0,(interY2 - interY1))*max(0.0,(interX2 - interX1));
double area1 = (bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1]);
double area2 = (bbox2[2]-bbox2[0])*(bbox2[3]-bbox2[1]);
iou = interArea / (area1 + area2 - interArea);
return iou;
}

vector<vector<double>> NMS(vector<vector<double>>& bbox, double& th) {
vector<vector<double>> ret;
sort(bbox.begin(), bbox.end(), cmp);
int i = 0, numBbox = bbox.size();
bool *flag = new bool[numBbox]();
while (i < numBbox) {
if (flag[i]) {
i++;
continue;
}
ret.push_back(bbox[i]);
flag[i] = true;
for (int j = i + 1; j < numBbox; j++) {
if (!flag[j]) {
double iou = calcIOU(bbox[i], bbox[j]);
if (iou > th) flag[j] = true;
}
}
i++;
}
return ret;
}

int main(void) {
int n;
cin >> n;
double th;
cin >> th;
vector<vector<double>> bbox(n, vector<double>(5, 0));
for (int i = 0; i<n; i++)
for (int j = 0; j<5; j++)
cin >> bbox[i][j];

vector<vector<double>> resBbox = NMS(bbox, th);
for (int i = 0; i<resBbox.size(); i++) {
for (int j = 0; j<4; j++)
printf("%.1f ", resBbox[i][j]);
printf("%.1f\n", resBbox[i][4]);
}
//system("pause");
return 0;
}
#小米##笔试题目#
全部评论
我也是14,到底哪里错了呀😥
点赞 回复 分享
发布于 2020-09-15 20:37
我也14。所以不能c++吗
点赞 回复 分享
发布于 2020-09-15 20:38
多少有面试啊
点赞 回复 分享
发布于 2020-09-15 20:38
输出示例答案,AC14%,只输出得分最高的,也AC14%
点赞 回复 分享
发布于 2020-09-15 20:39
求第二题代码!
点赞 回复 分享
发布于 2020-09-15 20:39

相关推荐

2 1 评论
分享
牛客网
牛客企业服务