0 | { 1, 2, 3 } | 12 |
1 | { 1, 3, 2, 1, 1, 3 } | 36 |
2 | {1000, 999, 998, 997, 996, 995} | 986074810223904000 |
3 | { 1, 1, 1, 1 } | 2 |
UNIX> sh tests.sh 12 36 986069860049153880 2 7601258117112660 11654570193710400 92953772500680000 799967508480 307257753600 60949905408 37412763010118496 328150073180160 1386927360000000 103723200000 156764160000 33861058560 303992974847508480 14169988790496000 11069314249061376 65156670562135680 27311800320 2913258700800 4756917475143680 1331646220800000 86802030000 29030400000 UNIX>How do you know if your code is working? Call tests.sh and store its standard output to a file. Then compare the file with answers.txt. Let me do that here:
UNIX> sh tests.sh > junk.txt UNIX> diff junk.txt answers.txt 3c3 < 986069860049153880 --- > 986074810223904000 5,10c5,10 < 7601258117112660 < 11654570193710400 ...... UNIX>If the output is non-empty, it means that your program is not working correctly. You can try diff -y, and it will show you the tests that you got wrong, by lining up your file and the answers line-by-line:
UNIX> diff -y junk.txt answers.txt 12 12 36 36 986069860049153880 | 986074810223904000 2 2 7601258117112660 | 7654413768281280 11654570193710400 | 15347582148096000 92953772500680000 | 113868371313333000 799967508480 | 1371372871680 307257753600 | 460886630400 60949905408 | 81266540544 37412763010118496 37412763010118496 328150073180160 | 628954306928640 1386927360000000 | 2037934080000000 103723200000 103723200000 156764160000 | 250822656000 33861058560 | 60197437440 303992974847508480 303992974847508480 14169988790496000 | 14571784313856000 11069314249061376 | 11602061993668608 65156670562135680 | 65880633568381632 27311800320 | 36415733760 2913258700800 | 5098202726400 4756917475143680 4756917475143680 1331646220800000 1331646220800000 86802030000 | 115736040000 29030400000 29030400000 UNIX>As you can see, lines 3, 5, 6, 7, and so on differ. So look at lines three and five of tests.sh:
UNIX> sed -n 3p tests.sh ./a.out 2 UNIX> sed -n 5p tests.sh echo 12 63 11 57 71 33 47 47 44 65 | ./a.out - UNIX>You now have test cases to help you debug! If your output of tests.sh is the same as answers.txt, then you can be satisfied that you have solved the problem!
[If you want to prove that formally, you can. Suppose you have a collection of numbers whose product is P. And suppose you have two numbers in the collection, a and b such that a < b. If you increment a, you change the product from P to P(a+1)/a. If you increment b, you change the product to P(b+1)/b. Since a < b, we know that (a+1)/a > (b+1)/b. (If you are stuck on that, think about examples: 3/2 is definitely greater than 101/100, right?) That means incrementing a will increase the factor by the greatest amount.]
So, you need to find the minimum element and increment it. You can write your own code to do that in two lines. Or you can use the sort() method from C++'s algorithms package to sort the vector and then increment the first element.
Now how about calculating the product? Since you are told that the product will be ≤ 262, you need to use a 64-bit integer. In C++, this is a long long. Calculate your product using that, and you're done.