读懂题目,注意细节,用到set,string。本地运行没有错误,但是牛客网过的时候显示错误如下

数据分类处理

http://www.nowcoder.com/questionTerminal/9a763ed59c7243bd8ab706b2da52b7fd

用例:
24 7907 610 4359 55 812 3002 10706 2470 8332 8573 3840 8105 9213 10159 11882 6517 7357 6398 4586 215 3420 4927 7159 9414
10 85 122 46 55 110 47 77 119 50 58

对应输出应该为:

16 47 1 7 2470 55 1 3 55 58 1 18 4586 85 1 9 8573

你的输出为:

16 47 1 7 2470 55 1 3 55 58 1 18 4586 85 1 9 8573 20 4 3 1 4598 3 6047 6 7402 26 1 5 11269 47 1 3 6047 85 1 2 8539

本地运行与对应输出一致。如果有大佬知道为何,望指教

代码

#include<iostream>
#include<set>
#include<string>
#include<vector>
using namespace std;
int I[10000];
int tmp;
int a[10000];//记录以及各方便输出
int main() {
    int rn, in;

    set<int> r;
    //输入
    while (cin >> in)
    {
        for (int j = 0; j < in; j++)
        {
            cin >> I[j];
        }
        cin >> rn;

        for (int i = 0; i < rn; i++)
        {
            cin >> tmp;
            r.insert(tmp); //对r要去重排序,所以直接用set
        }
        for (int i = 0; i < r.size(); i++)
        {
            a[i] = 0;
        }
        int cnt = 0;//记录输出的第一个数
        int k = 0;
        for (set<int>::iterator it = r.begin(); it != r.end(); it++)
        {
            int itn = 0;//记录下来这个it有多少个
            tmp = *it;
            //r不一定是个位数,所以转为字符串查找子串
            //对tmp遍历I
            string str, s;
            str = to_string(tmp);
            bool flag = true;
            for (int j = 0; j < in; j++)
            {
                s = to_string(I[j]);
                if (s.find(str) != s.npos) {
                    if (flag)
                    {
                        cnt += 2;
                        flag = false;
                    }
                    cnt = cnt + 2;
                    itn++;
                }
            }
            a[k++] = itn;
        }
        cout << cnt << ' ';
        int i = 0;
        for (set<int>::iterator it = r.begin(); it != r.end(); it++)
        {
            if (a[i] != 0)
            {
                //输出r[i]是输入进来匹配的数,a[i]是存在几个即itn
                cout << *it << ' ' << a[i] << ' ';
                //输出index和数字
                string str;
                str = to_string(*it);
                for (int j = 0; j < in; j++)
                {
                    string s;
                    s = to_string(I[j]);
                    if (s.find(str) != s.npos) {
                        cout << j << ' ' << s << ' ';
                    }
                }
            }
            i++;
        }


    }




    return 0;
}
全部评论
您好,我和你这个题是同样的错误,但是不知道哪里错了,想问下你研究出来了吗?
1 回复
分享
发布于 2020-08-12 17:23
输出完之后再加个换行 cout<
点赞 回复
分享
发布于 2020-08-12 19:30
联易融
校招火热招聘中
官网直投

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务