当前位置:网站首页>7 矩阵中战斗力最弱的 K 行
7 矩阵中战斗力最弱的 K 行
2022-07-17 00:07:00 【DHU杨骅麟(紫外线过敏)】
7 矩阵中战斗力最弱的 K 行
作者: Turbo时间限制: 1S章节: 课程设计
问题描述 :
给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示。
请你返回矩阵中战斗力最弱的 k 行的索引(行号),按从最弱到最强排序。
如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。
军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。
示例 1:
输入:
5 5
1 1 0 0 0
1 1 1 1 0
1 0 0 0 0
1 1 0 0 0
1 1 1 1 1
3
输出:
2 0 3
解释:
每行中的军人数目:
行 0 有 2 人
行 1 有 4 人
行 2 有 1 人
行 3 有 2 人
行 4 有 5 人
从最弱到最强对这些行排序后得到 [2,0,3,1,4]
示例 2:
输入:
4 4
1 0 0 0
1 1 1 1
1 0 0 0
1 0 0 0
2
输出:
0 2
解释:
每行中的军人数目:
行 0 有 1 人
行 1 有 4 人
行 2 有 1 人
行 3 有 1 人
从最弱到最强对这些行排序后得到 [0,2,3,1]
输入说明 :
输入若干行:
第一行输入两个整数m和n,表示矩阵的行数和列数。
之后m行,每行输入n个整数(0或1)表示矩阵的元素。
最后一行输入一个整数k(1 <= k <= m).
提示:
2 <= n, m <= 100
1 <= k <= m
矩阵的元素 不是 0 就是 1
输出说明 :
输出一行k个整数,每个整数后跟一个空格。
输入范例 :
4 4
1 0 0 0
1 1 0 0
1 0 0 0
1 1 1 0
3
输出范例 :
0 2 1
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
int line= -1;
int number= 0;
};
bool cmp(student x, student y)
{
if (x.number == y.number)
return x.line < y.line;
else
return x.number < y.number;
}
int main()
{
int arr[500][500];
student m[100];
int x = 0;
int y = 0;
cin >> x >> y;
for (int i = 0; i < x; i++)
{
m[i].line = i;
for (int u = 0; u < y; u++)
{
cin >> arr[x][u];
if (arr[x][u] == 1)
{
m[i].number++;
}
}
}
int k = 0;
cin >> k;
sort(m, m + x, cmp);
for (int i = 0; i < k; i++)
{
cout << m[i].line;
cout << " ";
}
return 0;
}边栏推荐
- JS replaces a character in the string, and JS modifies the specified character in the string
- JS higher order function filter/map/reduce
- ES6 syntax -- Deconstruction assignment
- Red sun safety range 3
- How to install the pagoda panel on the server? (pagoda panel installation tutorial)
- Cento7安装mysql5.5以及升级5.7
- qs模块是?
- 时间戳转化时间
- 金融学 杂项记录
- 04-BTC-实现
猜你喜欢
随机推荐
05_ Review object defineProperty
object-fit:cover; It doesn't work in the applet. How to deal with the deformation of the applet image
El date picker time range control
Express项目创建以及其路由介绍
Red sun safety range 3
How to modify the color of a line of text in the elemtnui table (elemtnui table modifies the color of a line of text)
04_理解MVVM
07-BTC-挖矿
Single page application spa and multi page application MPa
软件漏洞分析入门(二)
2022.7.7 summary of some errors
Uni app wechat applet - Mall (3) - Mall Homepage
Summary of Applied Cryptography
If the website is hacked, what if you jump to other websites through Baidu / Sogou and other search keywords?
[SWPU 2019] network TTL encryption and some related knowledge
JS string to object JS object to string JS string to object
el-date-picker时间范围控制
Uni app wechat official account (4) - address management page
Solve the flashing of the menu switch at the bottom of applet customization
iptables和snort基本配置









