数组字符串题目c语言(急需)

 

提供思路:你可以弄个数组,用于统计每个编号出现的次数,循环输入的数据,把输入的数据作为数组的索引进行统计,第一次赋值为1,第二次在原有基础上++

学生参与课堂学习活动的次数是怎么输入的呢?没看到题目有相关功能。

我看不到图。把问题以文本写出来看看?

答案


#include <stdio.h>

int main()
{
    int max = 0, n;
    int activity[101];
    for (int i = 0; i <= 100; ++i)
        activity[i] = 0;


    printf("Please enter the numbers (separated by space): \n");
    do
    {
        scanf("%d", &n);
        if (n > 0 && n <= 100)
        {
            ++activity[n];
            if (activity[n] > max)
                max = activity[n];
        }
    } while (n > 0);
    
    printf("\nThe most active students: \n");
    for (int i = 0; i <= 100; ++i)
    {
        if (activity[i] == max)
            printf("%d ", i);
    }
    return 0;
}

// Ouput:
Please enter the numbers (separated by space):                                                                                                                                     
5 13 21 7 65 7 21 38 82 21 33 21 0                                                                                                                                                 
                                                                                                                                                                                   
The most active students:                                                                                                                                                          
21      

附注:求赞助积分和C币。加入CSDN将近20年了。最近几年忙小孩没登录。刚才搜索到一本电子书想下载,需要20积分/C币。已经收到8元了,还差12元。赞助多少都可以。多谢。

希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

struct Student
{
    int id;
    int cnt;
    struct Student* next;
};

void Insert(Student* pHeader, int id)
{
    if ((NULL == pHeader) || (0 == id)) { return; }

    bool bExist = false;
    Student* pCurrent = pHeader->next;
    while (NULL != pCurrent)
    {
        if (pCurrent->id == id) { pCurrent->cnt++; bExist = true; break; }

        pCurrent = pCurrent->next;
    }

    if (!bExist)
    {
        Student* pNew = (Student*)malloc(sizeof(Student));
        if (NULL != pNew)
        {
            pNew->id = id;
            pNew->cnt = 1;

            pNew->next = pHeader->next;
            pHeader->next = pNew;
        }
    }
}

void Delete(Student* pHeader)
{
    Student* pCurrent = pHeader->next;

    while (NULL != pCurrent)
    {
        pHeader->next = pCurrent->next;

        free(pCurrent);
        pCurrent = NULL;
    }
}

void Sort(Student* pHeader)
{
    Student* p = pHeader->next, *pre;
    Student* r = p->next;
    p->next = NULL;
    p = r;
    
    while (p != NULL) 
    {
        r = p->next;
        pre = pHeader;
        
        while ((pre->next != NULL) && (pre->next->cnt > p->cnt)) 
        {
            pre = pre->next;
        }
        p->next = pre->next;
        pre->next = p;
        p = r;
    }
}

void Analyze(Student* pHeader)
{
    Student* pCurrent = pHeader->next;

    int cnt = pCurrent->cnt;
    while (NULL != pCurrent)
    {
        if (pCurrent->cnt < cnt) { break; }

        printf("%d ", pCurrent->id);

        pCurrent = pCurrent->next;
    }
}

int main()
{
    Student Header = { 0 };

    while (1)
    {
        int id = 0;
        scanf_s("%d", &id);
        if (0 == id) { break; }

        Insert(&Header, id);
    }

    Sort(&Header);

    Analyze(&Header);

    Delete(&Header);

    return 0;
}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y