挖掘机技术哪家强
输入格式:
输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。
输出格式:
在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
输入样例:
6 3 65 2 80 1 100 2 70 3 40 3 0
输出样例:
2 150
int main()
{
int id, score;
int index = -1, temp_id = 1; //不初始化也行
int a[10001] = { 0 }; //记录总分
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> id >> score;
a[id] += score; //所有相同id的分数之和
if (a[id] >= index)
{
index = a[id]; //分数
temp_id = id; //编号
}
}
cout << temp_id << index;
return 0;
}
延伸:相同编号的合并,并且按大小输出。
输入例子:
4 0 1 0 2 1 2 3 4
输出例子:
0 3 1 2 3 4
int main()
{
int n;
while (cin >> n)
{
map<int, int> m;
while (n--)
{
int key, value;
cin >> key >> value;
if (!m[key])
m[key] = value;
else
m[key] += value; //不存在时赋值,存在时累加
}
for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
}
return 0;
} //
//map内部本身就是按照key的大小顺序进行存储的
浙公网安备 33010602011771号