20240602
第五題哪里不对啊
#include<iostream> #include<algorithm> #include<set> #include<string> #include<sstream> using namespace std; int main() { int n; scanf("%d",&n); set<int> st[n]; for(int i=0;i<n;i++) { int m; scanf("%d",&m); for(int j=0;j<m;j++) { int tmp; scanf("%d",&tmp); st[i].insert(tmp); } } int k; cin >> k; for(int i=0;i<k;i++) { int x,y; cin >> x >> y; x--;y--; int lx = st[x].size(); int ly = st[y].size(); set<int> tx = st[x]; set<int> ty = st[y]; //求两个set的交集res tx.insert(ty.begin(),ty.end()); double per = double(lx + ly - tx.size())/tx.size()*100; int pertwo=int(per); std::stringstream ss1; ss1<<pertwo; std::string str1 = ss1.str(); cout<<"0."<<pertwo; if(i!=k-1){ cout << endl; } } return 0; }
kdl666 • 11个月前
计算精度问题
zhenghaowen • 11个月前
先给你一组hack数据
zhenghaowen • 11个月前
Input
2
2 1 2
3 1 2 3
1
1 2
Output
0.67
zhenghaowen • 11个月前
你的输出是0.66
zhenghaowen • 11个月前
改一下这段代码
double per = double(lx + ly - tx.size())/tx.size()*100;
int pertwo=int(per);
std::stringstream ss1;
ss1<<pertwo;
std::string str1 = ss1.str();
cout<<"0."<<pertwo;
zhenghaowen • 11个月前
改成这样就过了
double per = double(lx + ly - tx.size())/tx.size();
printf("%.2lf",per);
zhenghaowen • 11个月前
那个ss1和str1变量完全都是没用的,可以省去,关键是那个强制转int类型的代码,会有精度丢失的
zhenghaowen • 11个月前