2

我想我的大脑只是抛出了一个内存不足异常并崩溃了,我的问题是我有一个SYSTEMTIME大小为 3 的类成员数组,它是用户定义的(从 .lua 读取)

SYSTEMTIME m_MatchTime[3];

然后从文件中以这种方式读取:

m_MatchTime[0].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstDay" ) );
m_MatchTime[0].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstHour" ) );
m_MatchTime[0].wMinute  = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstMinute" ) );

m_MatchTime[1].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondDay" ) );
m_MatchTime[1].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondHour" ) );
m_MatchTime[1].wMinute  = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondMinute" ) );

m_MatchTime[2].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdDay" ) );
m_MatchTime[2].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdHour" ) );
m_MatchTime[2].wMinute  = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdMinute" ) );

现在我有一个方法:

SYSTEMTIME cTime;
GetLocalTime( &cTime );

我必须从三个用户定义的时间中计算出哪个是 BEFORE 和更接近当前时间,然后计算到它的剩余时间,(请注意 Sunday = 0,Saturday = 6,还请注意,只有 wDayOfWeek、wHour 和 wMinute 必须是与到达最近的位置相比)

编辑:现在我奖励500 赏金以获得解决方案,请注意我想要的示例,

今天:第 4 天,第 3 小时,第 0 分钟,
日期:第 5 天,第 5 小时,第 30 分钟
距离日期的剩余时间为:1 天 2 小时 30 分钟

4

5 回答 5

5

鉴于问题域,似乎没有必要(甚至不希望)强制执行严格的时间排序,您只想找到一组时间中最接近给定标记值的时间。这将需要线性复杂性,但很容易实现。

我建议计算与已知纪元的时间差,在本例中为周日 00:00:00(以秒为单位),然后比较从该点开始的每个时间的差异,看看哪个最接近。

#include <Windows.h>
#include <algorithm>
#include <iostream>

long seconds_from_sunday_epoch(const SYSTEMTIME& t)
{
   size_t seconds = t.wDayOfWeek * 86400;
   seconds += t.wHour * 3600;
   seconds += t.wMinute * 60;
   return seconds;
}

size_t timediff_2(const SYSTEMTIME& t0, const SYSTEMTIME& t1)
{
   size_t seconds_diff = std::abs(
      seconds_from_sunday_epoch(t0) -
      seconds_from_sunday_epoch(t1));

   return seconds_diff;
}

int main()
{
   SYSTEMTIME m_MatchTime[3];


   // Monday: 00:00
   m_MatchTime[0].wDayOfWeek = 1;
   m_MatchTime[0].wHour = 0;
   m_MatchTime[0].wMinute = 0;

   // Sunday: 01:00
   m_MatchTime[1].wDayOfWeek = 0;
   m_MatchTime[1].wHour = 1;
   m_MatchTime[1].wMinute = 0;

   // Wednesday: 15:30
   m_MatchTime[2].wDayOfWeek = 3;
   m_MatchTime[2].wHour = 15;
   m_MatchTime[2].wMinute = 30;

   // Sunday 23:00
   SYSTEMTIME cTime;
   cTime.wDayOfWeek = 0;
   cTime.wHour = 23;
   cTime.wMinute = 0;

   std::cout << timediff_2(cTime, m_MatchTime[0]) << "\n";
   std::cout << timediff_2(cTime, m_MatchTime[1]) << "\n";
   std::cout << timediff_2(cTime, m_MatchTime[2]) << "\n";
}
于 2012-11-05T21:30:55.020 回答
3

所以问题是你坐在一个圆圈上,想知道从 d1 到 d2 的距离是向右(保持在同一周内)还是向左(一个值到下一个星期天)更短。

首先,您应该使用公式 min+hour*60+weekday*60*24 将日期转换为值。这将为您提供一周的时间。

#include <stdlib.h>
int minOfWeek (int d, int h, int m) {
  return d*60*24+h*60+m;
}

接下来找到最小距离:

const int minutesInWeek=60*24*7;
int bestDistance (int minutes1, int minutes2) {
  int d=abs (minutes1-minutes2);
  int dNext=minutesInWeek-d;
  return d<dNext?d:dNext;
}

因此,从您的实际时间计算 minOfWeek,将您一周内的所有 3 次喂给 bestDistance 并取最小的数字...

于 2012-11-05T22:08:52.360 回答
2

标准 C++ 库通过将比较日期的“魔法”转移到仿函数并使用std::sort采用自定义比较器的重载,让您可以相当优雅地解决这个问题。

以下是您可以使用很少几行代码的方法(链接到 ideone 上的快速测试):

class ClosestTo {
    int minute_now;
    int abs_minute(const SYSTEMTIME& t) const {
        return 60 * (24 * t.wDayOfWeek + t.wHour) + t.wMinute;
    }
    int diff_to_now(const SYSTEMTIME& t) const {
        int res = abs_minute(t) - minute_now;
        // Has the date passed this week?
       if (res < 0) {
            // Yes, the date has passed - move to next week:
            res += 7*24*60;
       }
        return res;
    }
public:
    ClosestTo(const SYSTEMTIME& now)
    :   minute_now(abs_minute(now)) {
    }
    // This is the operator the std::sort is going to call to determine ordering
    bool operator() (const SYSTEMTIME& lhs, const SYSTEMTIME& rhs) const {
        // Pick the date implying the shortest difference to minute_now
        return diff_to_now(lhs) < diff_to_now(rhs);
    }
};

就是这样,真的!有了这个比较器,您可以像这样对三个日期进行排序:

ClosestTo cmp(cTime);
sort(m_MatchTime, m_MatchTime+3, cmp);

现在最近的日期在索引零处:

SYSTEMTIME &nearest = m_MatchTime[0];
于 2012-11-09T19:00:30.277 回答
0

我为解决方案提供了一个算法,我知道它远非最专业的方法,但到目前为止它完美无缺。

int main()
{
SYSTEMTIME m_MatchTime[3];


// Monday: 00:00
m_MatchTime[0].wDayOfWeek = 1;
m_MatchTime[0].wHour = 22;
m_MatchTime[0].wMinute = 4;

// Sunday: 01:00
m_MatchTime[1].wDayOfWeek = 4;
m_MatchTime[1].wHour = 1;
m_MatchTime[1].wMinute = 0;

// Wednesday: 15:30
m_MatchTime[2].wDayOfWeek = 6;
m_MatchTime[2].wHour = 15;
m_MatchTime[2].wMinute = 30;

// Sunday 23:00
SYSTEMTIME cTime;
cTime.wDayOfWeek = 3;
cTime.wHour = 14;
cTime.wMinute = 5;

/*  std::cout << timediff_2(cTime, m_MatchTime[0]) << "\n";
std::cout << timediff_2(cTime, m_MatchTime[1]) << "\n";
std::cout << timediff_2(cTime, m_MatchTime[2]) << "\n";*/

vector<size_t>m_Time;
if( cTime.wDayOfWeek == 0 )
{
    for( int i =0; i<3; i++ )
    {
        if( cTime.wDayOfWeek >= m_MatchTime[i].wDayOfWeek )
            m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
    }

    if( m_Time.size() == 0 ) //trim right
    {
        for( int i =0; i<3; i++ )
        {
            if( cTime.wDayOfWeek <= m_MatchTime[i].wDayOfWeek )
                m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
        }
    }
}
else
{
    for( int i =0; i<3; i++ )
    {
        if( cTime.wDayOfWeek <= m_MatchTime[i].wDayOfWeek )
            m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
    }

    if( m_Time.size() == 0 ) //trim right
    {
        for( int i =0; i<3; i++ )
        {
            if( cTime.wDayOfWeek >= m_MatchTime[i].wDayOfWeek )
                m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
        }
    }
}


std::sort( m_Time.begin(), m_Time.end() );

SYSTEMTIME nearest;
if( m_Time.size() > 0 )
{
    for( int l=0; l<3; l++ )
    {
        if( timediff_2( cTime, m_MatchTime[l] ) == m_Time.at(0) )
        {
            nearest = m_MatchTime[l];
            break;
        }
    }
}

unsigned int manydaysleft = howmanydaysuntil(  nearest.wDayOfWeek , cTime.wDayOfWeek );
unsigned int manyhoursleft = howmanyhoursuntil(  nearest.wHour, cTime.wHour );
if( nearest.wHour < cTime.wHour ) //manydaysleft will always be > 0
    manydaysleft--;
unsigned int manyminutesleft = howmanyminutesuntil( nearest.wMinute, cTime.wMinute );
if( nearest.wMinute < cTime.wMinute )
    manyhoursleft--;



/*cout 
    << manydaysleft << endl
    << manyhoursleft << endl
    << manyminutesleft << endl;*/

cout << "CurrentTime\n"  
    << "Day:" << cTime.wDayOfWeek
    << "Hour:" << cTime.wHour
    << "Min:" << cTime.wMinute 

    << "\nDay:" << nearest.wDayOfWeek
    << "Hour:" << nearest.wHour
    << "Min:" << nearest.wMinute

    << "\nDay:" << manydaysleft
    << "Hour:" << manyhoursleft
    << "Min:" << manyminutesleft;

    return 0;
}
于 2012-11-07T22:05:01.110 回答
0

const unsigned n=3; //replace with actual array size

auto packtime = [](const SYSTEMTIME& t)->unsigned
{
    return t.wDayOfWeek*24*60 + t.wHour*60 + t.wMinute;
};
auto unpacktime = [](unsigned total)->SYSTEMTIME
{
    SYSTEMTIME ret;

    ret.wDayOfWeek = total/(60*24);
    total %= (60*24);
    ret.wHour = total/60;
    ret.wMinute = total%60;

    return ret;
};

unsigned const wraptime = 7*24*60;
unsigned targettime = packtime(cTime);

unsigned mintimedif = wraptime + 1;
unsigned mindifidx;
unsigned timedif;

for(unsigned i=0; i<n; ++i)
{
    timedif = packtime(m_MatchTime[i]);

    if(timedif < targettime)
        timedif = targettime - timedif;
    else
        timedif = wraptime - timedif + targettime;

    if(timedif < mintimedif)
    {
        mintimedif = timedif;
        mindifidx = i;
    }
}

SYSTEMTIME dif = unpacktime(mintimedif);

std::cout<<"Today: Day "<<cTime.wDayOfWeek<<" Hour "<<cTime.wHour<<" Minute "<<cTime.wMinute<<std::endl;
std::cout<<"Nearest day: Day "<<m_MatchTime[mindifidx].wDayOfWeek<<" Hour "<<m_MatchTime[mindifidx].wHour<<" Minute "<<m_MatchTime[mindifidx].wMinute<<std::endl;
std::cout<<"Difference: "<<dif.wDayOfWeek<<" days "<<dif.wHour<<" hours "<<dif.wMinute<<" minutes"<<std::endl;</code>
于 2012-11-09T05:33:03.960 回答