拼多多笔试第三题,只ac了15%,求大佬帮忙看看哪里错了??
#include <iostream>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<string>
using namespace std;
struct Node {
int num;
int times;
int min;
};
void MapSort(vector<Node> &map)
{
for (int i = 0; i<map.size() - 1; i++)
for (int j = i + 1; j < map.size(); j++)
{
if (map[j].times > map[i].times)
swap(map[j], map[i]);
else if (map[j].times == map[i].times&&map[j].min < map[i].min)
swap(map[j], map[i]);
}
}
int main() {
int num, k;
cin >> num >> k;
string input;
cin >> input;
vector<int> vec, help, jud(10, 0);
vector<Node> map;
string ans;
for (int i = 0; i < 10; i++)
{
map.push_back({ i, 0 ,20 });
}
for (int i = 0; i < input.size(); i++)
{
int key = input[i] - '0';
jud[key]++;
vec.push_back(key);
}
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
//数据处理
for (int i = 0; i < jud.size(); i++)
{
map[i].times = jud[i];
}
for (int i = 0; i < jud.size(); i++)
{
if (jud[i] != 0)
help.push_back(i);
}
int mid = help[help.size() / 2];
for (int i = 0; i < map.size(); i++)
{
if (map[i].times != 0)
map[i].min = abs(map[i].num - mid);
}
MapSort(map);
for (int i = 0; i < map.size(); i++)
cout << map[i].num<<' ' << map[i].times << ' '<<map[i].min << endl;
//改变数组
int NumLeft = k - map[0].times;
int sum = 0;
if (NumLeft > 0)
{
for (int i = 1; i < map.size(); i++)
{
int tar = map[i].num;
for (int j = 0; j < vec.size(); j++)
{
if (vec[j] == tar)
{
vec[j] = map[0].num;
map[i].times--;
NumLeft--;
sum += map[i].min;
}
if (map[i].times == 0 || NumLeft == 0)
break;
}
if (NumLeft == 0)
break;
}
}
for (int i = 0; i < vec.size(); i++)
{
char key = '0' + vec[i];
ans.push_back(key);
}
cout << sum << endl;
cout << ans;
system("pause");
}
#笔试拼多多##拼多多##笔试题目#
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<string>
using namespace std;
struct Node {
int num;
int times;
int min;
};
void MapSort(vector<Node> &map)
{
for (int i = 0; i<map.size() - 1; i++)
for (int j = i + 1; j < map.size(); j++)
{
if (map[j].times > map[i].times)
swap(map[j], map[i]);
else if (map[j].times == map[i].times&&map[j].min < map[i].min)
swap(map[j], map[i]);
}
}
int main() {
int num, k;
cin >> num >> k;
string input;
cin >> input;
vector<int> vec, help, jud(10, 0);
vector<Node> map;
string ans;
for (int i = 0; i < 10; i++)
{
map.push_back({ i, 0 ,20 });
}
for (int i = 0; i < input.size(); i++)
{
int key = input[i] - '0';
jud[key]++;
vec.push_back(key);
}
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
//数据处理
for (int i = 0; i < jud.size(); i++)
{
map[i].times = jud[i];
}
for (int i = 0; i < jud.size(); i++)
{
if (jud[i] != 0)
help.push_back(i);
}
int mid = help[help.size() / 2];
for (int i = 0; i < map.size(); i++)
{
if (map[i].times != 0)
map[i].min = abs(map[i].num - mid);
}
MapSort(map);
for (int i = 0; i < map.size(); i++)
cout << map[i].num<<' ' << map[i].times << ' '<<map[i].min << endl;
//改变数组
int NumLeft = k - map[0].times;
int sum = 0;
if (NumLeft > 0)
{
for (int i = 1; i < map.size(); i++)
{
int tar = map[i].num;
for (int j = 0; j < vec.size(); j++)
{
if (vec[j] == tar)
{
vec[j] = map[0].num;
map[i].times--;
NumLeft--;
sum += map[i].min;
}
if (map[i].times == 0 || NumLeft == 0)
break;
}
if (NumLeft == 0)
break;
}
}
for (int i = 0; i < vec.size(); i++)
{
char key = '0' + vec[i];
ans.push_back(key);
}
cout << sum << endl;
cout << ans;
system("pause");
}
#笔试拼多多##拼多多##笔试题目#