[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--;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值