hackerrank问题:给定一个字符串,由字母和数字组成,在给定的字符串中找到每个数字的频率

发布于 2025-02-05 02:37:31 字数 952 浏览 1 评论 0 原文

链接: https:///www.hackerrank.com/challenges.com/challenges/challenges/challenges/frequerquence/frequerquence-1/-1/问题?isfullscreen = true& h_r = next-challenge& h_v = zen& h_r = next-challenge& h_v = zen& h_r = next-challenge&amp

;这里错了吗?我的CodeBlocks给出了正确的输出,但在Hackerrank中,它的答案错误。

#include <string.h>`
#include <math.h>
#include <stdlib.h>

int main() {
    char s[1000];
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        a[m]=a[m]+1;
    }
    
    for(i=0; i<9; i++){
        printf("%d ", a[i]);
    }
     printf("%d", a[9]);
     return 0;
}```

link:https://www.hackerrank.com/challenges/frequency-of-digits-1/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen

where the wrong here? my codeblocks gives right output but in hackerrank it wrong answer.

#include <string.h>`
#include <math.h>
#include <stdlib.h>

int main() {
    char s[1000];
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        a[m]=a[m]+1;
    }
    
    for(i=0; i<9; i++){
        printf("%d ", a[i]);
    }
     printf("%d", a[9]);
     return 0;
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2025-02-12 02:37:31

如果将条件放置为M&gt; = 0和M&lt; 10,则a [m] ++。该代码可以正常工作。
工作代码。也更新为1001。

#include <string.h>`
#include <math.h>
#include <stdlib.h>

int main() {
    char s[1001] ;
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        if(m>=0 && m<10)
        a[m]++;
    }
    
    for(i=0; i<=9; i++){
        printf("%d ", a[i]);
    }
     return 0;
}

If you put the condition m>=0 and m<10 then a[m]++. The code will work fine.
Working code. Also updated size to 1001.

#include <string.h>`
#include <math.h>
#include <stdlib.h>

int main() {
    char s[1001] ;
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        if(m>=0 && m<10)
        a[m]++;
    }
    
    for(i=0; i<=9; i++){
        printf("%d ", a[i]);
    }
     return 0;
}

若有似无的小暗淡 2025-02-12 02:37:31

对于初学者,这些标头

#include <string.h>`
#include <math.h>
#include <stdlib.h>

是多余的。可以在不使用标头声明的情况下编写该程序。

相反,您需要包括标题&lt; stdio.h&gt;

#include <stdio.h>

数组 a 应该具有类型 size_t 。即使对于具有 1000 元素的字符数组, char> char 的对象也无法存储如此大的值。

数组的初始化 a 也可以更简单

size_t a[10] = { 0 };

,因为从作业中遵循字符数组 s 应该能够存储一个等于>>>>>的长度的字符串。 1000 。这意味着字符数组本身至少应具有 1001 字符,以便能够存储带有 1000 <的字符串的终止零字符'\ 0' /代码>字符。

这些语句

    int m = s[i]-48;
    a[m]=a[m]+1;

可能会导致访问数组调用未定义行为的数组之外的内存,因为没有检查当前字符是否包含数字。

该程序可以以以下方式查看。

#include <stdio.h>

int main( void ) 
{
    char s[1001];
    size_t digits[10] = { 0 };

    if ( scanf( "%1000[^\n]", s ) == 1 )
    {
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( '0' <= s[i] && s[i] <= '9' )
            {
                ++digits[s[i] - '0'];
            } 
        } 
    
        for ( size_t i = 0; i < 10; i++ )
        {
            printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );
        }
    }

    return 0;
}

例如,如果要输入字符串

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

printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );

12233344444555555555666666666666666777777777777777777777777777777777777777777777777777777777777777799999999999999

printf( "%zu ", digits[i] );

0 1 2 3 4 5 6 7 8 9 

For starters these headers

#include <string.h>`
#include <math.h>
#include <stdlib.h>

are redundant. The program can be written without using any declaration from the headers.

Instead you need to include the header <stdio.h>

#include <stdio.h>

The array a should have the type size_t. Even for the character array having 1000 elements an object of the type char can be unable to store such big values.

And the initialization of the array a can be simpler

size_t a[10] = { 0 };

Also as it follows from the assignment the character array s should be able to store a string with the length equal to 1000. It means that the character array itself shall have at least 1001 character to be able to store also the terminating zero character '\0' of a string with 1000 characters.

These statements

    int m = s[i]-48;
    a[m]=a[m]+1;

can result in accessing memory beyond the array a that invokes undefined behavior because there is no check whether the current character contains a digit.

The program can look the following way.

#include <stdio.h>

int main( void ) 
{
    char s[1001];
    size_t digits[10] = { 0 };

    if ( scanf( "%1000[^\n]", s ) == 1 )
    {
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( '0' <= s[i] && s[i] <= '9' )
            {
                ++digits[s[i] - '0'];
            } 
        } 
    
        for ( size_t i = 0; i < 10; i++ )
        {
            printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );
        }
    }

    return 0;
}

For example if to enter string "122333444455555666666777777788888888999999999" then the output will look like

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

If you need to output frequencies in one line then instead of this statement

printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );

just write

printf( "%zu ", digits[i] );

In this case for the above string the output will be

0 1 2 3 4 5 6 7 8 9 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文