为什么报错?实验6.3set

img


#include 
#include 
#include 
using namespace std;
int main()
{
    int N,M,K,x,y,j,i;
    double s,c;
    cin>>N;
    set<int>a[50];
    for(i=1;i<=N;i++)
    {
        cin>>M;
        for(j=0;j>x;
            a[i].insert(x);
        }
    }
    set<int>b;
    set<int>::iterator its;
    set<int>::iterator it;
    cin>>K;
    for(i=0;i0;
        cin>>x>>y;
        for(it=a[x].begin;it!=a[x].end;it++)
            b.insert(*it);
        for(its=a[y].begin;its!=a[y].end;its++)
        {
            b.insert(*its);
            if(*its==*it)
                c++;
        }
        s=c/b.size()*100%;
        printf("%.2s\n",s);
    }
    return 0;
}

有多处错误:
1.在头文件中应该包含 cstdio 而不是 stdio.h。
2.在使用迭代器时,需要在 begin() 后面加上括号
3在输出百分比时,需要用 %f%% 而不是 %s% 来表示百分数。
提供一份正确代码,望采纳

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

int main() {
    int N, M, K, x, y, j, i;
    double s, c;
    cin >> N;
    set<int> a[50];
    for (i = 1; i <= N; i++) {
        cin >> M;
        for (j = 0; j < M; j++) {
            cin >> x;
            a[i].insert(x);
        }
    }
    set<int> b;
    set<int>::iterator its;
    set<int>::iterator it;
    cin >> K;
    for (i = 0; i < K; i++) {
        c = 0;
        cin >> x >> y;
        for (it = a[x].begin(); it != a[x].end(); it++)
            b.insert(*it);
        for (its = a[y].begin(); its != a[y].end(); its++) {
            b.insert(*its);
            if (*its == *it)
                c++;
        }
        s = c / b.size() * 100.0;
        printf("%.2f%%\n", s);
    }
    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    抱歉,以上参考资料中没有与实验6.3的set操作相关的内容,无法针对该问题给出具体的解决方案。建议检查代码中是否有语法错误或逻辑错误,以及是否正确包含了所需的头文件和库文件等。另外,可以尝试使用调试工具对代码进行调试,以便定位具体的问题并进行修复。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^