小米9.15算法笔试
第一题 动规
第二题 NMS,为什么我用c++写只能ac14%?
第二题 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;
}
#小米##笔试题目##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;
}