perl – 帮我完成我应用的最后一部分?它通过强制解决每个可能的等式来解决第4频道上的任何倒计时数字游戏

perl – 帮我完成我应用的最后一部分?它通过强制解决每个可能的等式来解决第4频道上的任何倒计时数字游戏,第1张

概述对于那些不熟悉游戏的人.您将获得8个数字,并且必须使用, – ,/和*来达到目标​​. 因此,如果目标是254并且您的游戏数量是2,50,5,2,1,那么您将通过说5 * 50 = 250来正确回答问题.然后2 2是4.添加它以及得到254. 有些游戏视频在这里: Video 1 video 2 基本上我通过生成所有大小的所有权限来为数字和符号的所有权限强制使用游戏,并使用基本的inflix计算器 对于那些不熟悉游戏的人.您将获得8个数字,并且必须使用,–,/和*来达到目标​​.

因此,如果目标是254并且您的游戏数量是2,50,5,2,1,那么您将通过说5 * 50 = 250来正确回答问题.然后2 2是4.添加它以及得到254.

有些游戏视频在这里:

Video 1
video 2

基本上我通过生成所有大小的所有权限来为数字和符号的所有权限强制使用游戏,并使用基本的inflix计算器来计算解决方案.

然而,它包含一个缺陷,因为所有解决方案都解决如下:((((1 1)* 2)* 3)* 4).它没有排列括号,这让我很头疼.

因此我无法解决所有方程式.例如,给定

目标16和数字1,1它应该做(1 1 1 1)*(1 1 1 1)= 16时失败.

我喜欢它,有人可以帮助完成这个…用任何语言.

这是我到目前为止所写的:

#!/usr/bin/env perluse strict;use warnings;use Algorithm::Permute;# GAME ParaMETERS TO FILL INmy $target = 751;my @numbers = ( '2','4','7','9','1','6','50','25' );my $num_numbers = scalar(@numbers);my @symbols = ();foreach my $n (@numbers) {    push(@symbols,('+','-','/','*'));}my $num_symbols = scalar(@symbols);print "Symbol table: " . join(",",@symbols);my $lst = [];my $symb_lst = [];my $perms = '';my @perm = ();my $symb_perms = '';my @symb_perm;my $print_mark = 0;my $progress = 0;my $total_perms = 0;my @closest_numbers;my @closest_symb;my $distance = 999999;sub calculate {    my @oprms = @{ $_[0] };    my @ooperators = @{ $_[1] };    my @prms = @oprms;    my @operators = @ooperators;    #print "PERMS: " . join(",@prms) . ",OPERATORS: " . join(",@operators);    my $total = pop(@prms);    foreach my $operator (@operators) {        my $x = pop(@prms);        if ($operator eq '+') {            $total += $x;        }        if ($operator eq '-') {            $total -= $x;        }        if ($operator eq '*') {            $total *= $x;        }        if ($operator eq '/') {            $total /= $x;        }    }    #print "Total: $total\n";    if ($total == $target) {        #print "ABLE TO ACCURATELY SolVE WITH THIS ALGORITHM:\n";        #print "PERMS: " . join(",@oprms) . ",@ooperators) . ",TOTAL=$total\n";        sum_print(\@oprms,\@ooperators,$total,0);        exit(0);    }    my $own_distance = ($target - $total);    if ($own_distance < 0) {        $own_distance *= -1;    }    if ($own_distance < $distance) {        #print "found a new solution - only $own_distance from target $target\n";        #print "PERMS: " . join(",$own_distance);        @closest_numbers = @oprms;        @closest_symb = @ooperators;        $distance = $own_distance;    }    $progress++;    if (($progress % $print_mark) == 0) {        print "Tested $progress permutations. " . (($progress / $total_perms) * 100) . "%\n";    }}sub factorial {    my $f = shift;    $f == 0 ? 1 : $f*factorial($f-1);}sub sum_print {    my @prms = @{ $_[0] };    my @operators = @{ $_[1] };    my $total = $_[2];    my $distance = $_[3];    my $tmp = '';    my $op_len = scalar(@operators);    print "BEST SolUTION SO FAR: ";    for (my $x = 0; $x < $op_len; $x++) {        print "(";    }    $tmp = pop(@prms);    print "$tmp";    foreach my $operator (@operators) {        $tmp = pop(@prms);        print " $operator $tmp)";    }    if ($distance == 0) {        print " = $total\n";    }    else {        print " = $total (distance from target $target is $distance)\n";    }}# look for straight matchforeach my $number (@numbers) {    if ($number == $target) {        print "matched!\n";    }}for (my $x = 1; $x < (($num_numbers*2)-1); $x++) {    $total_perms += factorial($x);}print "Total number of permutations: $total_perms\n";$print_mark = $total_perms / 100;if ($print_mark == 0) {    $print_mark = $total_perms;}for (my $num_size=2; $num_size <= $num_numbers; $num_size++) {    $lst = \@numbers;    $perms = new Algorithm::Permute($lst,$num_size);    print "Perms of size: $num_size.\n";    # print matching symb permutations    $symb_lst = \@symbols;    $symb_perms = new Algorithm::Permute($symb_lst,$num_size-1);    while (@perm = $perms->next) {        while (@symb_perm = $symb_perms->next) {            calculate(\@perm,\@symb_perm);        }        $symb_perms = new Algorithm::Permute($symb_lst,$num_size-1);    }}print "exhausted solutions";print "CLOSEST I CAN GET: $distance\n";sum_print(\@closest_numbers,\@closest_symb,$target-$distance,$distance);exit(0);

以下是示例输出:

[15:53: /mnt/mydocuments/git_working_dir/countdown_solver$] perl countdown_solver.plSymbol table: +,-,/,*,+,*Total number of permutations: 93928268313Perms of size: 2.BEST SolUTION SO FAR: (2 + 4) = 6 (distance from target 751 is 745)BEST SolUTION SO FAR: (2 * 4) = 8 (distance from target 751 is 743)BEST SolUTION SO FAR: (4 + 7) = 11 (distance from target 751 is 740)BEST SolUTION SO FAR: (4 * 7) = 28 (distance from target 751 is 723)BEST SolUTION SO FAR: (4 * 9) = 36 (distance from target 751 is 715)BEST SolUTION SO FAR: (7 * 9) = 63 (distance from target 751 is 688)BEST SolUTION SO FAR: (4 * 50) = 200 (distance from target 751 is 551)BEST SolUTION SO FAR: (7 * 50) = 350 (distance from target 751 is 401)BEST SolUTION SO FAR: (9 * 50) = 450 (distance from target 751 is 301)Perms of size: 3.BEST SolUTION SO FAR: ((4 + 7) * 50) = 550 (distance from target 751 is 201)BEST SolUTION SO FAR: ((2 * 7) * 50) = 700 (distance from target 751 is 51)BEST SolUTION SO FAR: ((7 + 9) * 50) = 800 (distance from target 751 is 49)BEST SolUTION SO FAR: ((9 + 6) * 50) = 750 (distance from target 751 is 1)Perms of size: 4.BEST SolUTION SO FAR: (((9 + 6) * 50) + 1) = 751
解决方法 Here是Java applet( source)和JavaScript版本. 总结

以上是内存溢出为你收集整理的perl – 帮我完成我应用的最后一部分?它通过强制解决每个可能的等式来解决第4频道上的任何倒计时数字游戏全部内容,希望文章能够帮你解决perl – 帮我完成我应用的最后一部分?它通过强制解决每个可能的等式来解决第4频道上的任何倒计时数字游戏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1271269.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存