#1155. 数字的出现次数
数字的出现次数
题目描述
给定 个数字 ,每个数字的范围都是 之间,最后输出 之间每个数字的出现次数。
输入格式
第一行输入 ,注意
第二行输入 个空格隔开的数字 ,注意
输出格式
输出 个空格隔开的数字分别代表 之间每个数字的出现次数。
6
1 2 3 4 1 1
0 3 1 1 1 0 0 0 0 0
提示
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
// 10个变量定义并初始化
int num0 = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0;
int num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0;
// 大小为10的数组定义并初始化
int num[10];
for (int i = 0; i <= 9; i++)
{
num[i] = 0;
}
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
if (x == 0)
num0++; // num[x]++;
if (x == 1)
num1++; // num[x]++;
if (x == 2)
num2++; // num[x]++;
if (x == 3)
num3++; // num[x]++;
if (x == 4)
num4++; // num[x]++;
if (x == 5)
num5++; // num[x]++;
if (x == 6)
num6++; // num[x]++;
if (x == 7)
num7++; // num[x]++;
if (x == 8)
num8++; // num[x]++;
if (x == 9)
num9++; // num[x]++;
}
cout << num0 << " " << num1 << " " << num2 << " " << num3 << " " << num4 << " ";
cout << num5 << " " << num6 << " " << num7 << " " << num8 << " " << num9;
for (int i = 0; i <= 9; i++)
{
cout << num[i] << " ";
}
return 0;
}