c语言如何实现一次性输入两个数值

如题所述

如果是任意大小的数字,那么有些麻烦,可以设定两个默认值代表最多的数字和最多的次数。接着去轮询,并计数,如果次数大于默认值,替换即可。遍历完成即可输出那两个值。

如果是0~9,或者字母的话,比较简单。可以定义定长的数组,数组下标代表具体值,数组的内容代表值出现的次数,遍历一遍原数组,得到次数。遍历定长数组,得到值。

第一种方法代码如下,第二种自己琢磨吧。

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

int main(int argc, char const *argv[])
{
    int num = 0;    //用来存最多值的数值,默认为0
    int coumt = 0;  //用来存最多值的个数,默认为0
    int temp = 0;   //中间变量
    int n = 0;      //数组的个数
    int *p = NULL;  //开辟空间的首地址,等价于&a[0]

    printf("Pls enter the number of arrays:");
    while(1)
    {
        scanf("%d", &n);
        if(n <= 0)
            printf("Error is scanf,pls try again\n");
        else
            break; 
    }

    p = (int *)malloc(sizeof(int) * n);
    if(p == NULL)
    {
        printf("Error is malloc\n");
        return -1;
    }

    for (int i = 0; i < n; ++i)
    {
        printf("Pls enter the num for buf[%d]=", i+1);
        scanf("%d", &p[i]);
    }

    for (int i = 0; i < n; ++i) //简单的遍历查找
    {
        temp = 0;
        for (int j = i; j < n; ++j)
        {
            if (p[i] == p[j])
            {
                temp++;//计数
            }
        }
        if(coumt < temp)//如果次数大于默认值,替换
        {
            coumt = temp;
            num = p[i];
        }
    }
    
    printf("The most common number is %d and the coumt is %d\n", num, coumt);

    return 0;
}

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

int main(int argc, char const *argv[])
{
    int coumt = 0;  //用来存最多值的个数,默认为0
    int temp = 0;   //中间变量,用于计数
    int n = 0;      //数组的个数
    int *p = NULL;  //开辟空间的首地址,等价于&a[0]
    int *num = NULL; //开辟空间的首地址,等价于&a[0]
    int flag = 0;   //定义一个标志位,用于计数重复的次数的数值出现

    printf("Pls enter the number of arrays:");
    while(1)
    {
        scanf("%d", &n);
        if (n <= 0)
            printf("Error is scanf,pls try again\n");
        else
            break; 
    }

    p = (int *)malloc(sizeof(int) * n);//存放你要的数组
    if (p == NULL)
    {
        printf("Error is malloc for p\n");
        return -1;
    }

    num = (int *)malloc(sizeof(int) * n);//存放最多数值的数组,最坏情况,没有重复数字
    if (num == NULL)
    {
        printf("Error is malloc for num\n");
        return -1;
    }

    for (int i = 0; i < n; ++i)
    {
        printf("Pls enter the num for buf[%d]=", i+1);
        scanf("%d", &p[i]);
    }

    for (int i = 0; i < n; ++i) //简单的遍历查找,找出最大的次数
    {
        temp = 0;
        for (int j = i; j < n; ++j)
        {
            if (p[i] == p[j])
            {
                temp++;//计数
            }
        }
        if (coumt < temp)//如果次数大于默认值,替换
            coumt = temp;
    }

    for (int i = 0; i < n; ++i) //简单的遍历查找,找出重复的次数
    {
        temp = 0;
        for (int j = i; j < n; ++j)
        {
            if (p[i] == p[j])
            {
                temp++;//计数
            }
        }
        if (coumt == temp)//如果次数等于最大值,存储
        {
            num[flag] = p[i];
            flag++;
        }
    }
    
    for (int i = 0; i < flag; ++i)
        printf("The most common number is %d and the coumt is %d\n", num[i], coumt);   

    free(p);
    free(num);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答