1 题目


2 思路
本题采用一维数组,索引为种类,值进行累加计数即可
3 代码
#include <iostream>
using namespace std;
int main(){
int n,m;
cin>> n >> m;
int total[n];
// 初始化
for(int i = 0; i < n; i++){
total[i] = 0;
}
// 一维数组进行计数
for(int i = 0; i < m; i++){
for(int j =0; j < n; j++){
int num;
cin>> num;
total[j] += num;
}
}
// 找出最大值
int max = 0;
for(int i = 0; i < n; i++){
if(total[i] > max){
max = total[i];
}
}
// 输出最大值
cout<< max << endl;
// flag控制格式输出
int flag = true;
for(int i = 0; i < n; i++){
if(total[i] == max){
if(flag){
cout << i + 1;
flag = false;
}else{
cout<< ' ' << i + 1;
}
}
}
system("pause");
return 0;
}
该博客介绍了一种利用一维数组进行计数的方法,通过输入数据更新数组元素,实现不同种类的数量累计。代码中,首先初始化数组,接着读取数据并累加到对应索引的数组元素上,最后找出数组中的最大值并输出,同时输出具有最大值的索引。此方法适用于简单的计数场景。

191

被折叠的 条评论
为什么被折叠?



