注册 登录
编程论坛 C++教室

HDOJ 2017 题 题目见正文

Ljldym 发布于 2016-03-08 19:43, 2211 次点击
只有本站会员才能查看附件,请 登录

提交之后显示Runtime Error
(ACCESS_VIOLATION)      求助!
#include"iostream"
using namespace std;
int main()
{
    int n,count;
    while (cin >> n)
    {
        fflush(stdin);
        count = 0;
        char **p;
        p = (char **)calloc(n, sizeof(char));
        for (int i = 0; i < n; i++)
            p[i] = (char *)calloc(100, sizeof(char));
        for (int i = 0; i < n; i++)
        {
            gets(p[i]);
            fflush(stdin);
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; p[i][j] != '\0'; j++)
            {
                if (p[i][j] >= '0' && p[i][j] <= '9')
                    count++;
            }
            cout << count << endl;
            count = 0;
        }
    }
}
3 回复
#2
wmf20142016-03-08 19:59
应该不需要数组就可完成,提交的acm代码不能有清空键缓冲区的语句,否则会使判题系统提前结束,无法完成测试。
#3
rjsp2016-03-09 08:38
fflush(stdin); 这些都是哪个半吊子告诉你的呀

以下代码没测试,仅供参考
程序代码:
#include <iostream>
using namespace std;

int main( void )
{
    size_t n;
    cin >> n >> ws >> noskipws;
    for( size_t i=0; i!=n; ++i )
    {
        size_t count = 0;
        for( char c; cin>>c && c!='\n'; )
        {
            if( c>='0' && c<='9' )
                ++count;
        }
        cout << count << '\n';
    }

    return 0;
}

#4
wmf20142016-03-09 09:14
回复 3楼 rjsp
一举通过
1