第二行包含 n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。
5 2 3 3 1 3 4 2 5 2 3 5
2 3
5 3
1 1
4 1
1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。
参考代码:
#include <iostream>
using namespace std;
struct num{
int s;
int times = 1;
};//数字的结构体
int main(){
int n;
cin >> n;
num *t = new num[n];
for (int i = 0; i < n; i++)
{
cin >> t[i].s;
}
//对数组数字的大小进行排序
num temp;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (t[j].s>t[j + 1].s)
{
temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
//找到相同数字的改变次数,并留下一个表示次数,其余次数置为0
int x = 1;
for (int i = 1; i < n; i++)
{
if (t[i].s == t[i - 1].s){
x++;
t[i - 1].times = 0;
}
else{
t[i - 1].times = x;
x = 1;
}
}
//判断最后一个数
if (x != 1){
t[n - 1].times = x;
}
//对数组次数进行冒泡排序
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (t[j].times<t[j + 1].times)
{
temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
//输出结果
for (int i = 0; i < n; i++){
if (t[i].times!=0){
cout << t[i].s << " " << t[i].times << endl;
}
}
return 0;
}
提交结果:
代码长度 | 编程语言 | 评测结果 | 得分 | 时间使用 | 空间使用 |
---|
880B | C++ | 正确 | 100 | 31ms | 504.0KB |