#include <iostream>
#include <stdio.h>
#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%;
printf("%.2s\n",s);
}
return 0;
}
为什么报错?实验6.3set
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 乘风xs 2023-04-22 11:05关注
有多处错误:
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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 测距传感器数据手册i2c
- ¥15 RPA正常跑,cmd输入cookies跑不出来
- ¥15 求帮我调试一下freefem代码
- ¥15 matlab代码解决,怎么运行
- ¥15 R语言Rstudio突然无法启动
- ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
- ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
- ¥15 用windows做服务的同志有吗
- ¥60 求一个简单的网页(标签-安全|关键词-上传)
- ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法