努力敲代码的小慧 2024-10-25 19:01 采纳率: 50%
浏览 6

有人能给我看看这个要怎么写吗?我写了一个,但就是无法输出

img


这是要怎么写呀,我写了很多次,但就是无法输出,不知道问题出在哪里?可以提供一个参考代码吗?

  • 写回答

1条回答 默认 最新

  • micthis 2024-10-25 20:55
    关注

    c++的话最直接的方法用正则表达式:

    #include<iostream>
    #include<iterator>
    #include<string>
    #include<regex>
    
    using namespace std;
    
    int main()
    {
        int n;
        string s;
        regex r(R"([+-]?\d+)");
        
        getline(cin,s);
        sregex_iterator it=sregex_iterator(s.begin(),s.end(),r);
        sregex_iterator digit_end=sregex_iterator();
        n=distance(it,digit_end);
        cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            if(i!=0)
                cout<<" ";
            cout<<stoll(it->str());
            it++;
        }
        cout<<endl;
        
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月25日