流行测验:解决数学难题?

时间:2015-05-22 22:39:36

标签: algorithm math

我在网上发现了一篇关于越南三年级数学的文章。我想看看我们如何解决这个问题会很有趣。以数学或程序方式。

我为此编写了一个测试函数,可以随意使用任何您熟悉的编程语言:

var answer = [1,1,1,1,1,1,1,1,1,1];
var isCorrect = function (answer) {
    return ((((((((((((a[0]+13)*a[1])/a[2])+a[3])+12)*a[4])-a[5])-11)+a[6])*a[7])/a[8])-10) === 66;
};

规则是数组中不应有可重复的数字。 接受的号码是1-9。

4 个答案:

答案 0 :(得分:1)

from constraint import *

def formula(a, b, c, d, e, f, g, h, i):
    return abs(a + (13 * (b / c)) + d + (12 * e) - f -
               11 + ((g * h) / i) - 10 - 66) < 0.001

if __name__ == '__main__':
    all_vals = [float(i) for i in xrange(1, 10)]
    p = Problem()
    [p.addVariable(l, all_vals) for l in 'abcdefghi']

    p.addConstraint(AllDifferentConstraint())
    p.addConstraint(FunctionConstraint(formula), 'abcdefghi')
    result = p.getSolution()
    print [s for s in sorted(result.items(), key=lambda (k,v): k)]

答案是:

[('a', 9.0), ('b', 8.0), ('c', 6.0), 
 ('d', 2.0), ('e', 4.0), ('f', 1.0), 
 ('g', 5.0), ('h', 7.0), ('i', 3.0)]

答案 1 :(得分:1)

Maraca,您的MATLAB代码运行良好,但应更改S行以获得正确的算术结果。新代码是:

[{"asin": "0188399313", "categories": [["Baby"]], "description": "Wee-Go Glass baby bottles by LifeFactory (Babylife) are designed to grow with your child. The included clear cover can also serve as an easy to hold cup. Twist on the solid cap (sold separately) and use your bottles for storing juice or snacks. Perfect for a lunchbox or traveling. The bright colored silicone sleeve (patent pending) helps to protect the bottle from breakage and provides a great gripping surface and tactile experience during feeding. The bottle and sleeve can be boiled or put in the dishwasher together. They can also go in the freezer, making breast milk storage simple.", "title": "Lifefactory 4oz BPA Free Glass Baby Bottles - 4-pack-raspberry and Lilac", "price": 69.99, "imUrl": "http://ecx.images-amazon.com/images/I/41SwthpdD9L._SX300_.jpg", "brand": "Lifefactory", "related": {"also_bought": ["B002SG7K7A", "B003CJSXW8", "B004PW4186", "B002O3JH9Q", "B002O3NLIO", "B004HGSU28"], "also_viewed": ["B003CJSXW8", "B0052QOL1Q", "B004PW4186", "B00EN0OLZ8", "B00EN0OOQY", "B0049YS46K", "B00E64CBLM", "B00F9YOOS6", "B00AH9RPVQ", "B00BCU2R7G", "B002O3NLIO", "B008NZ4X2K", "B005NIDFEW", "B00DKPJCH4", "B00CZNGWWK", "B00DAKJIQ4", "B005CT55IQ", "B0049YRJM0", "B0071IEWD0", "B00E64CA68", "B00IUB3SKK", "B00A7AA6XY", "B001F50FFE", "B002HU9EO4", "B007HP11SQ", "B009WPUMX4", "B002O3JH9Q", "B00F2FT3K6", "B00I5CR35A", "B00BCTY5EK", "B002SG7K7A", "B00F2FLU2U", "B0062ZK0GQ", "B002UOFR66", "B0055LKQQ2", "B00A0FGN8I", "B00HMYCG2W", "B00DHFLUO0", "B0040HMPA2", "B00I5CT9XE", "B008B5MMNO", "B00BQYVNGO", "B00925WM28", "B00BGKC3EY", "B005Q3LSDO", "B0038JDVCE", "B0045I6IA4"], "bought_together": ["B002SG7K7A", "B003CJSXW8"], "buy_after_viewing": ["B003CJSXW8", "B0052QOL1Q", "B004PW4186", "B002SG7K7A"]},},
 {"asin": "0188399518", "categories": [["Baby"]], "description": "The Planet Wise Flannel Wipes are 10 super soft, color coordinated, high-quality wipes that fit perfectly into the Planet Wise Wipe PouchTM.", "title": "Planetwise Flannel Wipes", "price": 15.95, "imUrl": "http://ecx.images-amazon.com/images/I/41otjnA4OGL._SY300_.jpg", "brand": "Planet Wise", "related": {"also_bought": ["B00G96N3YY", "B003XSEV2O", "B000138GNY", "B005WWIE3G", "B005WWI0DA", "B005WWIMGA", "B00DS4WPNK", "B0039VCRPI", "B001QIN6ME", "B004GMGLN8", "B00CJ2OWUG", "B004BDNJW8", "B003N0JXSO", "B003X3R6TO", "B002T5Q01C", "B006J2U4T0", "B00JBJDEC2", "B00380LVLG", "B00GJVM2NW", "B003AJHDQW", "B0043EDGP0", "B0012IJBUE", "B001OI0YWG", "B005DL5970", "B00305GSKS", "B00CQ9UUK8", "B002Y27PQ4", "B00AH8J448", "B00A3JXVZY", "B00483GAJU", "B001J6O6B8", "B0019ID6G2", "B005DL7LGM", "B00APSYDXM", "B004D5KJJA", "B0021HR94K", "0757302661", "B001HX4DNE"], "also_viewed": ["B000138GNY", "B00G96N3YY", "B006J2U4T0", "B00GLBR3C0", "B0039VCRPI", "B0012IJBUE", "B001IA2XGK", "B001R2L6PS", "B003X3R6TO", "B00DS4WPNK", "B005WWIE3G", "B004BDNJW8", "B005WWIMGA", "B005WWI0DA", "B003XSEV2O", "B004A5VGPE", "B004A5TTGC", "B00JBJDEC2", "B004GMGLN8", "B009W6PM9M", "B004RGL9I0", "B0040IWMTK", "B00J3C0XC6", "B00HO1HBC8", "B00G96N18M", "B00A3V0XDA", "B0032AMM9M", "B001QIN6ME", "B00A3V0XEY", "B00CIUAB40", "B00J3C0Y1G", "B005IPE0KI", "B00L0W5B7Y", "B00FZGLKQW", "B00HBYOE3W", "B004A5TT4O", "B00B6KS578", "B002HOQOMA", "B004G792KW", "B002UD6BZS", "B002XVRKHK", "B002HOQOUW", "B002UD6C1Q", "B00APSYDXM", "B00H07AZ42", "B002LZX2BG", "B00BFQVZNO", "B00EZKNVYS", "B00DCLYYJW", "B00AHG8H1Q", "B002QZ64T8", "B003N0JXSO", "B003AJHDQW", "B00DN9NW32", "B00CHPJ0I4", "B005WXMJUY", "B00D4LFC3W"], "bought_together": ["B00G96N3YY", "B003XSEV2O"], "buy_after_viewing": ["B000138GNY", "B006J2U4T0", "B00G96N3YY", "B005WWIMGA"]},},
 {"asin": "0188399399", "description": "The Planet Wise Wipe PouchTM features our patent pending no-leak design so your wipes will stay moist and not wick into your diaper bag. Features a unique snap down design to eliminate wicking from the zipper area. Most standard size wipes will fit into our Wipe PouchTM perfectly! Pouches are made of the same high quality fabrics as our Wet Bags, this pouch will last and last. Pouches are anti-microbial too! After you are done with the baby years, keep this pouch to use for just about anything you need to transport: make-up, toiletries, anything. Made in the USA!", "title": "Planetwise Wipe Pouch", "price": 10.95, "imUrl": "http://ecx.images-amazon.com/images/I/61x8h9u6mxL._SY300_.jpg", "related": {"also_bought": ["B005WWI0DA", "B005WWIMGA", "B006J2U4T0", "B000138GNY", "B003XSEV2O", "B005WWIE3G", "B00DS4WPNK", "B0039VCRPI", "B003GSLCOG", "B0018B15FE", "B002MN3JY2"], "also_viewed": ["B005WWIMGA", "B00G96N3YY", "B00DAI76TC", "B0067GKHVS", "B005WWIE3G", "B004RGL9I0", "B000138GNY", "B00JBJDEC2", "B00CMCJ2AS", "B005WWI0DA", "B002UD6C16", "B00E401FVU", "B004GMGLN8", "B0012IJBUE", "B00BFQVZNO", "B006J2U4T0", "B000VV21G4", "B002HOQOUW", "B003X3R6TO", "B000GZJIVQ", "B0052AIF00", "B005GQ6606", "B003TZHCA4", "B001QIN6ME", "B003XSEV2O", "B00GLBR3C0", "B00DW9R03G", "B00J3C0XC6", "0188399542", "B00DQL6CIE", "B002UD6C1G", "B00A3V0XDA", "B0039VCRPI", "B00EZKNVYS", "B006QRFDHQ", "B00DS4WPNK", "B00CQ9UUK8", "B0087A2PKS", "B00GGNICSW", "B0037NXP18", "B000YCNLSC", "B00FJG945W", "B00FZGLKQW", "B004A5VGPE", "B00D4LFC3W", "B00G96N18M", "B00439UNR4", "B004A5TT4O", "B005DL7LGM", "B006SD2JUW", "B006Z6HXYY", "B006QRFDZI", "B00I0P7VYK", "B00AZWDM0I", "B000ZKHVMU", "B007799PGC", "B00H58UNMU", "B00JKUU3TE"], "buy_after_viewing": ["B005WWIMGA", "B00G96N3YY", "B00DAI76TC", "B0067GKHVS"]},, "categories": [["Baby"]]},
 {"asin": "0316967297", "description": "Hand crafted set includes 1 full quilt (76x86 inches) and 2 standard shams (20x26 inches). Face cloth and fill are 100 natural cotton. Prewashed for out of the bag comfort. Hand crafted with embroidery. Machine washable. Made in China", "title": "Annas Dream Full Quilt with 2 Shams", "price": 109.95, "imUrl": "http://ecx.images-amazon.com/images/I/51%2BZ1%2BNeukL._SY300_.jpg", "related": {"also_viewed": ["B009LTER3W", "B00575TI5Q", "B004NSYYJI"], "buy_after_viewing": ["B009LTER3W", "B001MX5EE6", "B00575TI5Q", "B0029009TG"]},, "categories": [["Baby"]]},
 {"asin": "0615447279", "categories": [["Baby"]], "description": "Thumbuddy To Love- The Binky Fairy helps children give up pacifier sucking without the fuss and tears. The adorable book comes with a matching Binky Fairy puppet and just like the Tooth Fairy, the book is read the night before so children understand where thier pacifiers will go. They awake to find an adorable Binky Fairy puppet under thier pillow knowing  that the Binky Fairy came! Each book comes with a success chart and stickers. Winner of  the PTPA Awards (Parent Tested, Parent Approved). Recommended for ages 2-4.", "title": "Stop Pacifier Sucking without tears with Thumbuddy To Love s Binky Fairy Puppet and Adorable Book", "price": 16.95, "imUrl": "http://ecx.images-amazon.com/images/I/51RKKENlq%2BL._SY300_.jpg", "brand": "", "related": {"also_bought": ["0979670004", "1601310234", "B005G172KE", "1575422573", "1905417896", "B0044D0HA2", "B001GQ2CPI"], "also_viewed": ["0979670004", "1575422573", "1493535943", "0615273645", "1601310234", "0992616727", "0615273793", "0763623644", "1907152962", "0992616719", "B0016ZX7AI", "0698400488", "1608442713", "0451416058", "1581176848", "B0072CTECY", "B005G172KE", "B0016P8K3W", "0375822704", "0979201004", "0961478020", "160131048X", "1572245859"], "bought_together": ["B005G172KE"], "buy_after_viewing": ["0979670004", "1575422573", "B005G172KE", "1493535943"]},}]

例如,一个测试解决方案是(a b c d e f g h i)=(3 2 1 5 4 7 9 8 6)

在代码中使用此更改,有128种独特的解决方案。

请参阅link

答案 2 :(得分:0)

Matlab / Octave(蛮力):

p = perms(1:9) ;
S = (((((((((((p(:,1)+13).*p(:,2))./p(:,3)+p(:,4))+12).*p(:,5))-p(:,6))-11)+p(:,7)).*p(:,8))./p(:,9))-10) == 66 ;
sum(S)
p(S,:)

我在八度音乐在线执行它,它表明有 144个解决方案只需要一小部分时间来计算。 (http://octave-online.net初始弹出窗口看起来很可怕,但它只是试图帮助,点击它并将整个程序代码复制到底部的控制台行中并点击[返回]以查看所有144个解决方案,有时你得到错误&#34;有效载荷太大&#34;但它只显示,数学在后台工作正常。)

<强>解释

对于数学问题,显然一种旨在解决这些问题的编程语言是一个很好的选择,并且可以从你的肩膀上完成很多工作,所以我使用Octave虽然我是Java程序员,这里是该计划的作用:

  1. 1:9会提供一个数字为19(1 x 9)的向量
  2. perms()计算此向量的所有排列并将其作为矩阵返回(9!x 9)
  3. ;会抑制输出
  4. p =置换矩阵存储在p
  5. p(:,n)访问n的{​​{1}}列,因此所有解决方案都会立即计算
  6. p.*确保单元格乘法而不是标准矩阵乘法
  7. 结果向量与./的比较会返回一个逻辑向量,该向量存储在66中,当方程为真且{{1}时,它就像一个S的向量否则(9!x 1)
  8. 1输出解决方案的数量
  9. 0返回之前的等式为真的所有行,解决方案(144 x 9)

答案 3 :(得分:0)

我想可能有一种更有效的方法解决这个问题但是,scince只有9个变量,其中有9个可能值,而且我们有计算机,蛮力。

我们的想法是为每个排列尝试check()函数,即你可以将数字1到9放在数组a []中。这意味着9!迭代= 362880我的笔记本电脑可以在几秒钟内完成。

我构建了recursove permutate(): 对于每个级别它,inmutate函数循环数字1到9,将它们中的每一个放在数组的级别位置(但仅当数字不在先前位置时)并且自己调用下一级别。在级别9中,数组已满,因此可以调用检查功能。

这是java中的代码:

public class Test {
    static float[] a = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
private static int countSolutions = 0;
public static void main(String[] args){
    permutate(0);
    System.out.println(countSolutions);
}

public static boolean permutate( int level){
    boolean isCorrect = false;
    if(level == 9){
        isCorrect = check();
        if(!isCorrect){
        }
    }
    else 
        for(int i = 1; i <= 9; i++){
            int j = 0;
            for(; j < level && a[j]!=i; j++);
            if( j == level){
                a[level] = i;
                isCorrect = permutate( level + 1);
            }
        }
    return isCorrect;
}

private static boolean check() {
    float result = ((((((((((((a[0]+13)*a[1])/a[2])+a[3])+12)*a[4])-a[5])-11)+a[6])*a[7])/a[8])-10);
    if ( result == 66){
        countSolutions ++;
        for (int i = 0; i < 9; i++) {
            System.out.print(a[i]+",");
        }
        System.out.println();
        return true;
    }else{
        return false;
    }
}

}

结果(144):

1.0 2.0 4.0 7.0 5.0 8.0 3.0 6.0 9.0

1.0 2.0 7.0 5.0 3.0 4.0 9.0 8.0 6.0

...

...