【问题标题】:Count frequency of digits in string计算字符串中数字的频率
【发布时间】:2018-12-01 18:31:57
【问题描述】:

我需要实现一个可以计算字符串中位数的函数。所以对于数字,但也适用于类似的东西:aD23b。如果我能让它工作......它应该看起来像:
输入:0912302
输出:
0:2
1:1
2:2
3:1
4:0
5:0
6:0
7:0
8:0
9:1

不幸的是,此时我无法编写任何有效的代码...我的基本想法是:使用循环检查输入中的每个字符,如果是数字,则将其存储在第二个数组中(比如说频率)。我遇到的问题是我需要以某种方式将每个字符转换为整数,或者以某种方式能够计算出如何 通常每个数字都会出现......我希望这可能会起作用,但它根本不起作用:

我忘了说我是编程初学者,所以如果您能给我一些提示和解释,我将不胜感激。

void calc_occurrences(int s[], int occurrences[])
{
int i = 0;
    int j;
    int count = 0;
    while (s[i] != '\0') {
        if (isdigit(s[i])) {
            for (j = 0; occurrences[j] != '\0'; j++) {
                occurrences[j] = s[i];
            }
        }
        i++;
        for (j = i + 1; s[j] != '\0'; j++) {
            if (isdigit(s[i]) == isdigit(s[j])) {
                count++;
                occurrences[j] = 0;
            }
        }

        if(occurrences[i] != 0) {
            occurrences[i] = count;
        }
    }
}

【问题讨论】:

  • “此时我无法编写任何可行的代码”好吧,发布您拥有的任何内容......然后我们可以帮助您修复它
  • 如果您想要最有效的解决方案,请考虑使用称为 map 的数据结构,它本质上保存键值对。虽然它可能非常低效,但您可能希望从使用数组来映射您的事件开始。
  • @ionizer A map 不是 C 中的标准类型,就像在 C++ 中一样
  • 我并不是说要使用库或其他任何东西,只是说数据结构。

标签: c string loops frequency digits


【解决方案1】:

制作一个数组来计算每个相关字符的频率。

类似这样的:

#include <stdio.h>

void count_freq(char* str, int freq[10])
{
    int i = 0;
    while(str[i])  // Loop to end of string
    {
        if (str[i] >= '0' && str[i] <= '9') // Check that the character is in range
        {
            ++freq[str[i]-'0'];  // notice the -'0' to get in range 0..9
        }
        ++i;
    }
}

int main(void) {
    int freq[10] = {0};             // Array to count occurence
    char str[] = "0034364hh324h34"; // Input string

    count_freq(str, freq);          // Calculate frequency

    for (int i=0; i < 10; ++i)      // Print result
    {
        printf("%d: %d\n", i, freq[i]);
    }
    return 0;
}

输出:

0: 2
1: 0
2: 1
3: 4
4: 4
5: 0
6: 1
7: 0
8: 0
9: 0

【讨论】:

  • 所以 while(str[i]) 与 while(str[i] != '\0') 相同?还有一个问题 ++freq[str[i]-'0'];做 ?我得到了 str[i]-'0' 的部分,这样你就解决了将 char 转换回整数的问题,但是 ++freq 是什么意思?
  • @Saty 是的 while(str[i])while(str[i] != '\0') 相同
  • @saty ++XX = X + 1; 相同
  • 边际 - 它不会影响我的投票或 OP 的接受,所以这是你的电话。
  • @4386427 好的,所以我仍在尝试了解您如何将数字存储在 freq[] 中...如果我理解正确,freq[10] = {0} 所以每个元素都是 0 . 如果 s[] 是索引 i 处的数字,则将 + 1 添加到索引 s[i] 处的 freq[] - '0'。所以如果 s[i] = '3' 那么你将它转换为 int 所以你有 3 并将 +1 添加到 freq[3] = 0 + 1 ?
【解决方案2】:

你可以有一个大小为 10 的整数数组,所有索引中都存储 0。然后,当您发现一个数字时,您可以增加相应索引中的数字。

例如,当你看到一个“0”时,你可以做 arr[0]++;.

另外,您可以使用 isdigit() 函数检查字符是否为数字。

【讨论】:

    【解决方案3】:

    PS :我知道,我在回答一个旧帖子,但我在 HackerRank 上做了一些挑战,我设法解决了这个几乎完全正确的问题,以防它可能对某人有所帮助,因为我在我的代码。

    /* Problem: hackkerrank.com/challenges/frequency-of-digits-1/problem */
    
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char    *s;
        int   *arr;
        int    i;
    
        i = 0;
        s = (char*)malloc(sizeof(char));
        scanf("%s", s);
        arr = (int*)malloc(10 * sizeof(int));
        while(i < 10)
        {
            *(arr + i) = 0;
            i++;
        }
        i = 0;
        while (i < strlen(s))
        {
            if (*(s + i) >= '0' && *(s + i) <= '9')
            {
                (*(arr + (*(s + i) - '0'))) += 1;
    
            }
            i++; 
        }
        i = 0;
        while (i < 10)
        {
            printf("%d ", *(arr + i)); // As HackerRank problem wanted the output format.
            // printf("%d: %d\n", i, *(arr + i));  As you wanted it
            i++;
        }
        return (0);
    }
    

    【讨论】:

    • s = (char*)malloc(sizeof(char)); scanf("%s", s); 内存分配太小导致UB。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2017-04-19
    • 2023-03-25
    • 2011-10-06
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多