首页 \ 问答 \ 具有上限和下限的Powerset(Powerset with upper and lower limits)

具有上限和下限的Powerset(Powerset with upper and lower limits)

我有一个过程,我需要找到符合特定标准的所有最佳组合。 Powersets是我认为我正在寻找的,但我想建立一些过滤器以避免数百万的结果。

例如,我有以下对象:

public class CableReel
    {
        public string ReelId { get; set; }
        public int Length { get; set; }
    }

我想获得上述目标的幂集,其中组合长度也满足下限和上限。

以下列表:

“a”,5200“b”,2500“c”,1000

我想加入以下限制,最小3000,最大5000.我想只看到BC组合。

我找到了以下用于返回通用powersets的代码:

public IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
    {
        return from m in Enumerable.Range(0, 1 << list.Count)
               select
                 from i in Enumerable.Range(0, list.Count)
                 where (m & (1 << i)) != 0
                 select list[i];
    }

我只是想知道如何调整它以完成我想要做的事情?

解:

使用Alex Sikilinda的建议,我想出了:

var ls = new List<CableReel>
            {
                new CableReel{Length = 5,ReelId = "A"},
                new CableReel{Length = 3,ReelId = "B"},
                new CableReel{Length = 2,ReelId = "C"},
                new CableReel{Length = 7,ReelId = "e"},
                new CableReel{Length = 3,ReelId = "f"},
                new CableReel{Length = 2,ReelId = "g"}

            };
    var p = GetPowerSet(ls).Where(psl => psl.Sum(ps => ps.Length) > 5 && psl.Sum(ps => ps.Length) < 8);

I have a process where I need to find all of the best combinations that fit a certain criteria. Powersets are what I think I am looking for, but i'd like to build in some filters in to avoid millions of results.

For example I have the following Object:

public class CableReel
    {
        public string ReelId { get; set; }
        public int Length { get; set; }
    }

I'd like to get a powerset of the above object where the combined lengths meet a lower limit and also an upper limit as well.

The following list:

"a", 5200 "b", 2500 "c", 1000

I'd like to put in the following limits, min 3000, max 5000. I'd like to then only see the BC combination.

I have found the following code for returning generic powersets:

public IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
    {
        return from m in Enumerable.Range(0, 1 << list.Count)
               select
                 from i in Enumerable.Range(0, list.Count)
                 where (m & (1 << i)) != 0
                 select list[i];
    }

I was just wondering how this might be adapted to accomplish what I am looking to do?

Solution:

Using Alex Sikilinda's advice I came up with:

var ls = new List<CableReel>
            {
                new CableReel{Length = 5,ReelId = "A"},
                new CableReel{Length = 3,ReelId = "B"},
                new CableReel{Length = 2,ReelId = "C"},
                new CableReel{Length = 7,ReelId = "e"},
                new CableReel{Length = 3,ReelId = "f"},
                new CableReel{Length = 2,ReelId = "g"}

            };
    var p = GetPowerSet(ls).Where(psl => psl.Sum(ps => ps.Length) > 5 && psl.Sum(ps => ps.Length) < 8);

原文:https://stackoverflow.com/questions/30464368
更新时间:2023-09-25 21:09

最满意答案

你应该检查fopen文档中的fopen

在更新模式('+')中,可能会执行输入和输出,但是如果没有对fflush,fseek,fsetpos或rewind的干预调用,则输出不能跟随输入,并且输入不能在没有调用fseek ,fsetpos或倒带,除非输入操作遇到文件结尾。

阅读和写作可能会被缓冲,但仍共享一个文件位置。 切换模式而不提醒运行时间(使用fseek )可能会导致缓冲。

所以线

fseek(fup, (i * sizeof(employee)), SEEK_SET);
fwrite(&temp, sizeof(employee), 1, fup);
fread(&temp, sizeof(employee), 1, fup);

在写入和读取之间需要多一个fseek


You should check the fine print in the fopen documentation:

In update mode ('+'), both input and output may be performed, but output cannot be followed by input without an intervening call to fflush, fseek, fsetpos or rewind, and input cannot be followed by output without an intervening call to fseek, fsetpos or rewind, unless the input operation encountered end of file.

Reading and writing might be buffered, but still share a single file position. Switching modes without alerting the runtime (using fseek) could mess up the buffering.

So the lines

fseek(fup, (i * sizeof(employee)), SEEK_SET);
fwrite(&temp, sizeof(employee), 1, fup);
fread(&temp, sizeof(employee), 1, fup);

need one more fseek between write and read.

相关问答

更多
  • 当且仅当 stdout引用可搜索的流时,才能成功使用stdout fseek 。 如果你的程序的标准输出到终端或类似的东西(这通常是默认的)或管道,那么fseek将会失败。 如果因为您执行了程序并将其输出重定向而转到某个文件,那么fseek可以像处理任何其他文件一样工作。 任何时候你打电话给fseek ,你都应该检查它的返回值,看它是成功还是失败。 假设 stdout是可搜索的,可能是不好的做法,因为它通常是一个糟糕的假设。 fseek和打印'\b'都不是你想要做的一个好方法。 而不是打印最终的逗号,然后尝 ...
  • fseek (fp, 0, position); 肯定是错的, fseek的论据应该是: 文件句柄。 这个职位。 “whence”(从位置计算)。 换句话说,它应该是: fseek (fp, position, SEEK_SET); 而且,顺便说一句,您通常应该始终检查可能失败的函数的返回代码,即使您认为它不会发生。 并且您可能希望根据当前标准将position为long int 。 它可能不会对小文件产生任何明显的差异,但是一旦你开始处理大于普通整数可以处理的文件,你就会遇到麻烦。 fseek (fp ...
  • 你不是很清楚你想要做什么,但无论如何这是我的猜测:你正在从文件中读取数据块,每个字节长度为256字节: char pinakas[256]; //this is the memory buffer for one page int y = ...; //this is the page number int p; fseek(fp, 256*y, SEEK_SET); fread(pinakas, 256, 1, fp); for (p = 0; p < 256; p++) printf(" % ...
  • 你得到这种结果,因为这不是获得磁盘大小的正确方法。 如果要检查磁盘大小,应在磁盘fd上发出一个ioctl(),请求为BLKGETSIZE64(以及指向long long的参数)。 long long disk_size; ioctl(, BLKGETSIZE64, &disk_size); 如果您对文件系统大小(可能不同)或文件系统上的空白空间感兴趣,请使用statvfs()调用。 You're getting this sort of results because that's no ...
  • 您可能想要更改来电 int strcmp(const char *s1, const char *s2); 成为呼叫 int memcmp(const void *s1, const void *s2, size_t n); 这将解决这个问题,只要你不在这些(非0终结)的char数组上使用str*()系列函数的其他成员。 注意 :然而, memcmp() 总是比较传递的字符数作为第三个参数( n )。 这可能不是你想要的。 更新: 另外(作为上述两种调用的混合),还有: int strncmp(cons ...
  • 你应该检查fopen文档中的fopen : 在更新模式('+')中,可能会执行输入和输出,但是如果没有对fflush,fseek,fsetpos或rewind的干预调用,则输出不能跟随输入,并且输入不能在没有调用fseek ,fsetpos或倒带,除非输入操作遇到文件结尾。 阅读和写作可能会被缓冲,但仍共享一个文件位置。 切换模式而不提醒运行时间(使用fseek )可能会导致缓冲。 所以线 fseek(fup, (i * sizeof(employee)), SEEK_SET); fwrite(&temp, ...
  • 添加一个open(filename,O_RDONLY); 打电话给第一个例子,对我来说都很好。 我怀疑你的问题是因为调用lseek(0, 100, SEEK_CUR); ,这是要求标准输入的寻求。 你不能总是寻求标准 - 如果你有: cat file | ./my_program 然后标准输入是一个fifo,你不能寻求。 如果我在我的系统上执行此操作,则返回-1 ,查找失败,并显示“非法搜索”错误。 这可能是您遇到的问题,您可能没有注意到,因为您没有在示例中检查搜索调用的返回值。 请注意,如果您有: ...
  • 如果您不打算仅附加到文件,请不要以附加模式打开文件。 从fopen的POSIX参考: 打开具有追加模式的文件(作为mode参数中的第一个字符)将导致对文件的所有后续写入被强制转换为当前的文件结束, 而不管是对fseek()的中间调用 。 看起来你正在寻找r+模式。 Don't open the file in append mode if you don't plan on only appending to it. From the POSIX reference for fopen: Opening a ...
  • 在第二个清单中有这一行: fp = fopen(fp_string, "wb+"); 您将文件截断为零长度。 你要: fp = fopen(fp_string, "rb+"); 参考: fopen - 打开一个流 With this line in the second listing: fp = fopen(fp_string, "wb+"); You're truncating the file to zero length. You want: fp = fopen(fp_string, "rb ...
  • 我的文件依次有32个浮点数。 更具体地说,您的文本文件包含32个浮点数的行,格式为文本到9个位置; 每行在末尾都有一个行尾字符。 有了这些信息,您可以通过先将其除以32来计算第k个数的偏移量,以获得该行,然后将行数乘以单个行的大小,并在当前行中添加偏移量,如这个: #define NUM_SIZE 9 #define ROW_NUM_COUNT 32 #define ROW_SIZE (ROW_NUM_COUNT*NUM_SIZE+1) ... int k = 123; // The index of th ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。