algorithm

Basic optimization — pairing widgets and rotors

只愿长相守 提交于 2021-02-16 20:09:06
问题 I know little of optimization problems, so hopefully this will be didactic for me: rotors = [1, 2, 3, 4...] widgets = ['a', 'b', 'c', 'd' ...] assert len(rotors) == len(widgets) part_values = [ (1, 'a', 34), (1, 'b', 26), (1, 'c', 11), (1, 'd', 8), (2, 'a', 5), (2, 'b', 17), .... ] Given a fixed number of widgets and a fixed number of rotors, how can you get a series of widget-rotor pairs that maximizes the total value where each widget and rotor can only be used once? 回答1: What you have is a

Strategy for detecting (via Reflection) if Enum is of type “Flags” in C#

痴心易碎 提交于 2021-02-16 18:26:09
问题 I'm using Reflection to read types inside an assembly (to generate code). I can see that some enumerations should have been marked with the [Flags] attribute but whoever wrote those enums forgot to add this attribute. Is there any reliable way of detecting when an enum can be considered a "Flags" enum? My strategy at the moment is to read the enum in descending order, and checking if the value of element(last -1) * 2 == element(last). That works great in most cases, except when I have enums

How to remove all instances of a duplicate from a vector<int> [duplicate]

余生颓废 提交于 2021-02-16 17:57:12
问题 This question already has answers here : How to remove duplicated items in a sorted vector (7 answers) Closed yesterday . I've been trying to solve an "easy" leetcode question for two hours now where I need to remove every instance of an int that appears more than once then add the non dupes together. I've tried about 10 different ways and I can only get it to go down to one copy of each int. Here is the nicest solution I've written, but given input {1,2,2,3,4} it would return {1,2,3,4}

Proper way to initialize a std::array from a C array

冷暖自知 提交于 2021-02-16 16:14:29
问题 I'm getting an array from a C API, and I'd like to copy this to a std::array for further use in my C++ code. So what is the proper way of doing that ? I 2 uses for this, one is: struct Foo f; //struct from C api that has a uint8_t kasme[32] (and other things) c_api_function(&f); std::array<uint8_t, 32> a; memcpy((void*)a.data(), f.kasme, a.size()); And this class MyClass { std::array<uint8_t, 32> kasme; int type; public: MyClass(int type_, uint8_t *kasme_) : type(type_) { memcpy((void*)kasme

Cases where the greedy algorithm fails the 0-1 knapsack p‌r‌o‌b‌l‌e‌m

空扰寡人 提交于 2021-02-16 15:17:29
问题 I'm looking for a case where the greedy algorithm of picking items of the highest value/weight ratio that has weight < max weight and putting it in the knapsack first won't work. Does anyone have examples? Because otherwise, the worst case for greedy would be O(nlogn) (nlogn to sort in descending value/weight and n to go through it) while the dynamic programming way's worst case would be O(nW), making greedy faster when logn < W. 回答1: Consider a backpack with capacity 4, and items with the

Increase precision in DirectX with high range positions?

依然范特西╮ 提交于 2021-02-16 13:12:34
问题 I am creating an little game in c++ with directx 9 with a randomly created world and have an issue when the player goes far from the 3d origins (0,0,0) 3d rendering become very imprecise causing visual issues. I think it's because the values sent to the shader are floats and floats become less precise when increasing. I though about a solution to move all the models instead of moving the camera but it makes more calculating, I 'm afraid about the possible performances decrease. Is there a

Increase precision in DirectX with high range positions?

假装没事ソ 提交于 2021-02-16 13:10:12
问题 I am creating an little game in c++ with directx 9 with a randomly created world and have an issue when the player goes far from the 3d origins (0,0,0) 3d rendering become very imprecise causing visual issues. I think it's because the values sent to the shader are floats and floats become less precise when increasing. I though about a solution to move all the models instead of moving the camera but it makes more calculating, I 'm afraid about the possible performances decrease. Is there a

Using Binary Search with Vectors.

﹥>﹥吖頭↗ 提交于 2021-02-16 08:55:32
问题 I'm trying to implement an algorithm that for each string in the first vector it does a binary search in the second vector and will output "YES:" if it finds a match or "No:" otherwise. Right now with my program my algo always outputs "NO:" and I can't find out what's going wrong. Any hints or tips would be appreciated. My Binary search: bool binary_search(const vector<string>& sorted_vec, string key) { size_t mid, left = 0 ; size_t right = sorted_vec.size(); // one position passed the right

Using Binary Search with Vectors.

不打扰是莪最后的温柔 提交于 2021-02-16 08:55:17
问题 I'm trying to implement an algorithm that for each string in the first vector it does a binary search in the second vector and will output "YES:" if it finds a match or "No:" otherwise. Right now with my program my algo always outputs "NO:" and I can't find out what's going wrong. Any hints or tips would be appreciated. My Binary search: bool binary_search(const vector<string>& sorted_vec, string key) { size_t mid, left = 0 ; size_t right = sorted_vec.size(); // one position passed the right

Algorithm to find nth root of a number

倖福魔咒の 提交于 2021-02-16 04:42:02
问题 I am looking for an efficient algorithm to find nth root of a number. The answer must be an integer. I have found that newtons method and bisection method are popular methods. Are there any efficient and simple methods for integer output? 回答1: #include <math.h> inline int root(int input, int n) { return round(pow(input, 1./n)); } This works for pretty much the whole integer range (as IEEE754 8-byte double s can represent the whole 32-bit int range exactly, which are the representations and