[Arrays]D. Liang 6.7 Counting single digits.c

Description

Write a program that reads in n integers between 0 and 9 and displays the count for each number.

Input

The first line is a positive integer t for the number of test cases.
Each test case contains two lines. The first line is an integer n (0<n<=100). The second line contains n integers between 0 and 9 .

Output

For each test case, outputs each distinct number (in increasing order) and its count seperated by one blank in one line.

Sample Input

2
3
1 7 1
7
5 6 5 6 6 5 5

Sample Output

1 2
7 1
5 4
6 3

My code:

//   Date:2020/4/18
//   Author:xiezhg5 
#include<stdio.h>
int main()
{
    int n;
    int s[100];//存储输入的数据
    int s1[10];//数组中0-9数据的种类
    scanf("%d", &n);//输入第一行数据
    while (n > 0)
    {
        int a, k = 0, l = 0, b;
        scanf("%d", &a);//输入第二行数据
        for (int i = 0; i < a; i++) {//输入第三行数据
            scanf("%d", &b);
            s[i] = b;
        }
        //找到第三行数据中存在的种类
        for (int i = 0; i <=9; i++) {//遍历数字0-9
            for (int j = 0; j < a; j++) {
                if (i == s[j])
                {
                    s1[k] = i;
                    k++;
                    break;
                }
            }
        }
        //计算第三行各种数据出现的次数
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < a; j++)
                if (s[j] == s1[i])l++;
            printf("%d %d\n",s1[i],l);   //注意输出格式 
            l = 0;
        }
        n--;
    }
}

发布了245 篇原创文章 · 获赞 255 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105602817