gpt4 book ai didi

c++ - 如何将 C++ 排序函数转换为 Swift?

转载 作者:搜寻专家 更新时间:2023-11-01 06:28:21 25 4
gpt4 key购买 nike

我将 C++ 编码转换为 Swift 编码。但是,我无法完全理解这个函数 sort(v.begin(), v.end());。但是,我已经写下了我从中了解到的内容。

问题是 swift 代码给出的输出与 C++ 代码不同。## Heading ##

C++

#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <array>

using namespace std;
typedef long long ll;

ll solve(ll N,ll K,vector<ll > &token){

ll ans=0;

vector< pair<ll, int> > v;

for(int i = 0; i < N; i++)

v.push_back( make_pair(token[i], N - i));

sort(v.begin(), v.end());

for(int i = 0; i < N; i++){
if( K >= v[i].second * v[i].first){
K -= v[i].second * v[i].first;
ans += v[i].second;
}
else{
ans += K / v[i].first;
K = 0;
}
}
return ans;
}

int main(){
ll N = 57;
ll K = 877914575;

static const int arr[] = {436426,979445,648772,690081,933447,190629,703497,47202,407775,894325,963982,804784,968417,302156,631932,735902,895728,78537,723857,330739,286918,329211,539679,238506,63340,686568,361868,660016,287940,296263,224593,601449,836991,890310,823355,177068,11431,8580,291757,449218,374934,594328,163676,829355,996221,899080,195922,531545,748511,34067,575467,338674,284691,206504,999835,262034,344965};

vector<ll> token (arr, arr + sizeof(arr) / sizeof(arr[0]) );
cout<<solve(N,K,token);
}

Swift Code(我试过转换):

import Foundation

typealias ll = Int64

func solve(_ N:ll, K:ll, token:inout [ll])->ll{

var ans:ll = 0
var K = K
var v = [(first:ll,second:ll)]()

for i in 0..<N{

v.append((token[Int(i)],N-i))
}
v = v.sorted(by: { (element1, element2) -> Bool in

return element1.first > element2.first
})

for i in 0..<N{

if(K >= ll(v[Int(i)].second) * v[Int(i)].first){

K -= ll(v[Int(i)].second) * v[Int(i)].first
ans += ll(v[Int(i)].second)
}else{
ans += K/v[Int(i)].first
K = 0
}
}
return ans
}

func main(){

let N:ll = 57;
let K:ll = 877914575;
var token:[ll] = [436426,979445,648772,690081,933447,190629,703497,47202,407775,894325,963982,804784,968417,302156,631932,735902,895728,78537,723857,330739,286918,329211,539679,238506,63340,686568,361868,660016,287940,296263,224593,601449,836991,890310,823355,177068,11431,8580,291757,449218,374934,594328,163676,829355,996221,899080,195922,531545,748511,34067,575467,338674,284691,206504,999835,262034,344965]
print(solve(N, K: K, token: &token))

}

main()

关于上述代码,C++ 将输出输出到 1637,而 Swift 将输出输出到 1438。但是如果没有 Sorting,它们会给出相同的数字 1612

我认为我在对 v 的值进行排序时做错了。但是,我不确定下面的 Swift 代码是否等同于 C++ 排序。

在上面的代码中排序:

C++:

sort(v.begin(), v.end());

swift

v = v.sorted(by: { (element1, element2) -> Bool in

return element1.first > element2.first
})

为什么这些程序给出不同的值?我该如何解决?

更新:

输入:

let N:ll = 57;
let K:ll = 877914575;
var token:[ll] = [436426,979445,648772,690081,933447,190629,703497,47202,407775,894325,963982,804784,968417,302156,631932,735902,895728,78537,723857,330739,286918,329211,539679,238506,63340,686568,361868,660016,287940,296263,224593,601449,836991,890310,823355,177068,11431,8580,291757,449218,374934,594328,163676,829355,996221,899080,195922,531545,748511,34067,575467,338674,284691,206504,999835,262034,344965]

预期结果:

1637

最佳答案

This文章解释了 sort 的默认实现方式适用于 C++ 中的成对 vector ,它只是根据第一个元素按升序对 vector 进行排序。

您的 Swift 代码执行相反的操作,按降序排序。只需更改 return element1.first > element2.firstreturn element1.first < element2.first你应该很好。

顺便说一句,您不应该只是在不同语言之间逐字转换代码。转换算法,而不是 C++ 代码。

关于c++ - 如何将 C++ 排序函数转换为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com