Keen06 最近的时间轴更新
Keen06

Keen06

V2EX 第 548073 号会员,加入于 2021-06-11 11:43:35 +08:00
Keen06 最近回复了
谢谢分享,今年硕士毕业,看了您的经历也对以后的工作生活有了一些参考
2022-11-19 17:55:38 +08:00
回复了 movq 创建的主题 程序员 为什么 Java 父类构造函数调用被重写的方法会调用到子类的
@geelaw 谢谢回复,我设想了一下在虚函数中调用另一个虚函数的场景,感觉可能修改虚表指针相比于继续将该虚函数解析为非虚函数更简单,修改虚表指针只需要在构造函数调用的第一个虚函数那里修改即可,后面自然可以调用合适的虚函数。再次感谢!
2022-11-18 13:29:29 +08:00
回复了 movq 创建的主题 程序员 为什么 Java 父类构造函数调用被重写的方法会调用到子类的
@geelaw 你好,你说的两种情况,当编译器直接将虚函数当作非虚函数解析时会出现什么问题呢?我没有想明白,希望解答一下,十分感谢
2022-11-16 17:46:22 +08:00
回复了 diandian666 创建的主题 程序员 十年程序员难倒了一个算法上面,真的老了
我写了一个暴力解法,时间复杂度 O(n^m), n 、m 分别表示数组 1 和数组 2 的元素个数, 空间复杂度 O(m)。
两个简单例子可以跑通, 第一个例子时间复杂度约为 3.99084e+39 ,显然超时了。。。
代码如下:本来想发 gist 链接,但网站提示我像在 spamming
'''
#include<iostream>
#include<vector>
#include<list>
#include<cmath>

using namespace std;

void helper(vector<double>& arr1,int n, vector<double>& arr2, int start, int m, vector<list<double>>& res,bool& isok){
//暴力法:将 arr2 中的 m 个数分成 n 堆,编号 0~n-1 堆,堆 i 中数的和等于 arr1[i]
//对于 arr2 中的每个数有 n 中选择,放入哪个堆中,穷举加回溯
//时间复杂度为 O(n^m),空间复杂度为解空间树的深度 O(m)
if(isok) return; //isok 表示是否找到解
if(start==m) {//因为两个数组总的和相等,并且在递归过程中 arr1 数组中个数都不会<0,所以如果遍历到最后说明 arr2 中所有数都放在正确的堆中
isok = true;
return;
}
for(int i=0;i<n;++i){
if(arr1[i]>arr2[start]||fabs(arr1[i]-arr2[start])<0.00001){//arr1[i]>=arr2[start]时可以放入 arr2[start]
res[i].push_back(arr2[start]);
arr1[i] -= arr2[start];
helper(arr1,n,arr2,start+1,m,res,isok);//递归查找解
arr1[i] += arr2[start];//回溯
if(!isok) res[i].pop_back();//若未找到解则回溯
else return;//找到则直接返回
}
}
}
int main(){
// vector<double> arr1 = {62.13,26.67,17.76};
// vector<double> arr2 = {24.92,5.88,5.04,3.64,3.45,3.36,2.8,2.8,2.52,2.24,
// 2.24,2.24,1.96,1.96,1.8,1.68,1.4,1.4,1.4,1.2,1.2,1.15,1.12,1.12,1.12,1.12,
// 1.12,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.56,0.56,0.56,0.56,0.56,
// 0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.4,0.4,0.4,0.4,0.4,0.28,0.28,0.28,0.28,
// 0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,
// 0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28};
//采用这组数据时,n=3,m=83,时间复杂度为 O(n^m)3.99084e+39


// vector<double> arr1 = {52.7,8.96};
// vector<double> arr2 = {21.44,6.72,5.44,5.12,4.48,3.20,2.24,1.92,1.92,1.92,1.28,
// 1.28,1.00,0.96,0.50,0.32,0.32,0.32,0.32,0.32,0.32,0.32};

vector<double> arr1 = {23.17,3.2,1.22,0.32};
vector<double> arr2 = {7.36,4.16,3.20,1.69,1.28,1.28,0.96,0.96,0.90,0.64,0.64,0.64,
0.50,0.50,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32};

int n = arr1.size();
int m = arr2.size();
cout<<"len of arr1 is "<<n<<endl;
cout<<"len of arr2 is "<<m<<endl;
cout<<"n^m is "<<pow(n,m)<<endl;
vector<list<double>> res(n);
bool isok = false;
helper(arr1,n,arr2,0,arr2.size(),res,isok);
for(int i=0;i<n;++i){
cout<<arr1[i]<<":\n";
double sum = 0;
for(double j:res[i]){
cout<<j<<' ';
sum += j;
}
cout<<'\n';
bool correct = fabs(sum-arr1[i])<0.000001?true:false;//验证解法是否正确
cout<<"sum is "<<sum<<", ";
cout<<"the result is "<<(correct?"correct":"wrong")<<'\n';
}

return 0;
}
'''
2022-11-16 16:47:29 +08:00
回复了 CEBBCAT 创建的主题 V2EX TIP - 如何在回复中贴代码
2022-11-16 16:39:38 +08:00
回复了 CEBBCAT 创建的主题 V2EX TIP - 如何在回复中贴代码
2022-04-08 10:22:05 +08:00
回复了 zhengxiexie 创建的主题 V2EX v2ex 图片上传终极解决方法
2022-04-08 10:21:15 +08:00
回复了 zhengxiexie 创建的主题 V2EX v2ex 图片上传终极解决方法
test
[Imgur]( )
2022-04-08 10:14:48 +08:00
回复了 isCyan 创建的主题 V2EX 攻略:教你如何在 V2EX 发图片/插链接/插代码/插视频(第二版)
多谢大家回复了
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5878 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 1374ms · UTC 02:20 · PVG 10:20 · LAX 19:20 · JFK 22:20
Developed with CodeLauncher
♥ Do have faith in what you're doing.