大输入的数字频率程序 [英] Digit frequency program for large input

查看:58
本文介绍了大输入的数字频率程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下程序,以找出字符数组中每个数字出现的次数.

I have written the below program to find out number of times each digit is occurring in the array of characters.

int main(){

    char s[2000],count,j=0;
    fgets(s,2000,stdin);
    for(int i=0;i<=9;i++)
    {
        count=0;j=0;
        while(*(s+j))
        {
            if(isdigit(*(s+j)))
            {
                if(i==(*(s+j)-'0'))
                    count++;
            }
            j++;    
        }
     printf("%d ",count);    
    }
    return 0;
}

但是它不能用于大量输入.

But it ain't working for large input.

b3n47b5xf13qlx233rg4u2c949i623e34nt5661se06b675utbpy258wz633855846l761d61x340h1vn19w191sj18v2u333556bh6m5uc4u050am05p961dhmpu6iq4667zg9 

预期输出是

5 10 5 12 8 11 15 4 4 6 

但是我得到的输出是

5 10 5 12 7 11 13 3 4 5 

有人可以帮我找出我哪里出了问题吗?

Can anyone help me out in finding where I have gone wrong?

推荐答案

除了DYZ的答案之外,您的逻辑也很复杂.您无需同时测试if(isdigit(*(s+j)))if(i==(*(s+j)-'0')),只需要重构代码并提供单个测试并使用频率阵列即可.

In addition to DYZ's answer, your logic is convoluted. You don't need to test both if(isdigit(*(s+j))) and if(i==(*(s+j)-'0')), you simply need to refactor you code and provide a single test and use a frequency array.

(一个由10个元素初始化为全零的简单数组,在找到该数字时,您将递增与每个数​​字相对应的索引,从而导致该数字的计数在完成时出现在相应的索引处)

(a simple array with 10 elements initialized to all zero where you increment the index that corresponds to each digit when that digit is found resulting in the count of that digit being present at the corresponding index when done)

#include <stdio.h>
#include <ctype.h>

#define MAXC 2048   /* if you need a constant, #define one (or more) */
#define NDIGIT 10

int main (void) {

    char s[MAXC] = "";
    size_t digits[NDIGIT] = {0};    /* declare a 'frequency array' */

    while (fgets (s, MAXC, stdin)) {    /* read all blocks of data */
        char *p = s;                    /* pointer to s */
        while (*p) {                    /* for each char in s */
            if (isdigit (*p))           /* Am I a digit? */
                digits[*p - '0']++;     /* increment value at index */
            p++;                        /* increment pointer */
        }
    }

    for (int i = 0; i < NDIGIT; i++)
        printf (" %zu", digits[i]);
    putchar ('\n');

    return 0;
}

或者没有ctype.h

#include <stdio.h>

#define MAXC 2048   /* if you need a constant, #define one (or more) */
#define NDIGIT 10

int main (void) {

    char s[MAXC] = "";
    size_t digits[NDIGIT] = {0};    /* declare a 'frequency array' */

    while (fgets (s, MAXC, stdin)) {    /* read all blocks of data */
        char *p = s;                    /* pointer to s */
        while (*p) {                    /* for each char in s */
            if ('0' <= *p && *p <= '9') /* Am I a digit? */
                digits[*p - '0']++;     /* increment value at index */
            p++;                        /* increment pointer */
        }
    }

    for (int i = 0; i < NDIGIT; i++)
        printf (" %zu", digits[i]);
    putchar ('\n');

    return 0;
}

使用/输出示例

在两种情况下,输入的输出均为:

In both cases, the output with your input is:

 5 10 5 12 8 11 15 4 4 6

仔细检查一下,如果还有其他问题,请告诉我.

Look things over and let me know if you have further questions.

这篇关于大输入的数字频率程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆