dsstjqsr631426 2017-10-25 16:47
浏览 19
已采纳

如何找到最高的5个数字序列[关闭]

I have this large string of digits:

$number = "8457714236241996394662789446446900947844174208436229465708413283480252308452309445136821833388596362971481609898423885940334748909310590559808872696932821556902064811261967616862184374009875724727718022667764327386516693129548512719134416243929994231994949382960356875888556457289812078753217535142126801211014214834504421933614164095445014707761460891338932623869783278796726454120129597585667668220935747858847437582800078002228689051590879937597834754848624741161377581359919801031273163364064818325258392890356143251007563549777054047013154878246432673611603091925150515368164497803349723366818641042829792279918398010084882019400234971502609827514132560077285356201267749465601230620222274625799272352699855298156629748948075668465722353974463067725471326703029315330935768523979276352367926340029415999734624584287325909545543547035525815285397244049320738084432091566581396643275969932094091735878708555576059445021204851541525428226299437613379041397828790357002353728103827178553574459018352";

I want to find a sequence of 5 numbers which creates the highest number. I supposed I have to iterate at all numbers then if the sum of numbers is bigger than previous one I have to save it and check it again, but with minus 1 previous and + 1 next. I just have no idea how to put this into script. How can I do it?

  • 写回答

1条回答 默认 最新

  • dongpu3792 2017-10-25 17:15
    关注

    First, you need to make a loop. The loop needs to execute a number of times equal to the length of the input string minus the length of the substring you want to evaluate. Calculate that length before constructing the loop.

    $digits = 5;
    $count = strlen($number) - $digits;
    

    Then initialize a maximum value. Loop from zero to the count you calculated, and take substrings starting from the position indicated by your loop increment variable. Compare them to your previous maximum value and overwrite that value with the current substring if it is greater.

    $max = 0;
    
    for ($i = 0; $i <= $count; $i++) {
        $substr = substr($number, $i, $digits);
        $max = max($max, $substr);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式