有没有人知道将数字均匀分配到一定数量的容器中的方法,确保容器的总值尽可能均匀?
编辑:"尽可能",我的意思是如果按X容器分配,每个容器的总数将接近总平均值.
现在我只是对数字数组进行排序(降序),然后将它们的值无关地分配到容器中.分配到三个容器中的一组1000,200,20,1000等于[2000],[200],[20].
我想做的是:
Example
Set of numbers: 10 30 503 23 1 85 355
If I were to distribute these into three containers I would just pick the highest first and then distribute them as I go, like this:
Cont 1 = 503
Cont 2 = 355
Cont 3 = 85 + 30 + 23 + 10 + 1
This will give the best possible distribution that you can get with the values provided.
Run Code Online (Sandbox Code Playgroud)
但我不知道在代码中表达这一点的简洁方法.
想法?
我在创建设置大小的客户区时遇到了一些问题.AdjustWindowRect()将无法正常工作,所以我决定尝试手动计算窗口的宽度和高度.
这也不起作用,我想知道为什么所以我检查了我以前考虑边界等的值.
#include <iostream>
#include <Windows.h>
int main(void)
{
std::cout << "GetSystemMetrics(SM_CYEDGE) = " << GetSystemMetrics(SM_CYEDGE) << std::endl;
std::cout << "GetSystemMetrics(SM_CXEDGE) = " << GetSystemMetrics(SM_CXEDGE) << std::endl;
std::cout << "GetSystemMetrics(SM_CYBORDER) = " << GetSystemMetrics(SM_CYBORDER) << std::endl;
std::cout << "GetSystemMetrics(SM_CXBORDER) = " << GetSystemMetrics(SM_CXBORDER) << std::endl;
std::cout << "GetSystemMetrics(SM_CYCAPTION) = " << GetSystemMetrics(SM_CYCAPTION);
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
GetSystemMetrics(SM_CYEDGE) = 2
GetSystemMetrics(SM_CXEDGE) = 2
GetSystemMetrics(SM_CYBORDER) = 1
GetSystemMetrics(SM_CXBORDER) = 1
GetSystemMetrics(SM_CYCAPTION) = 22
Run Code Online (Sandbox Code Playgroud)
我很确定窗户的边框不是那么薄.我究竟做错了什么?
编辑1:
最初我的窗口使用了WS_OVERLAPPED样式.由于AdjustWindowRect不允许将该样式与它一起使用,因此我构造了我想要的相同类型的窗口:(WS_BORDER | WS_CAPTION | WS_SYSMENU).这是我在调用AdjustWindowRect和AdjustWindowRectEx时使用的相同样式(这个样式以NULL作为扩展样式,因为我不使用任何样式).这给了我正确的宽度,但高度缺少几个像素.
RECT rect = …Run Code Online (Sandbox Code Playgroud) 我一直在修补读取文件(用Unicode编码的文本文件),出于某种原因,我在输出的开头有一个问号.
这是代码.
#include <iostream>
#include <Windows.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
HANDLE hFile = CreateFile(L"dog.txt",
GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
_setmode(_fileno(stdout), _O_U16TEXT); //Making sure the console will
//display the wide characters
//correctly. See below for link
LARGE_INTEGER li;
GetFileSizeEx(hFile,&li);
WCHAR* pBuf = new WCHAR[li.QuadPart / sizeof(WCHAR)]; //Allocating space for
//the file.
DWORD dwRead = 0;
BOOL bFinishRead = FALSE;
do
{
bFinishRead = ReadFile(hFile,pBuf,li.QuadPart,&dwRead,NULL);
} while(!bFinishRead);
pBuf[li.QuadPart / sizeof(WCHAR)] = 0; //Making sure the end of the …Run Code Online (Sandbox Code Playgroud)