1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <cmath> #include <functional> #include <iostream> #include <iomanip> double function0(int j, int i) { if(i == 0 || j == 1) return 1; if(i == 1 || j == 0) return j; if(i > 0) return j * function0(j, --i); return 1 / (function0(j, -i)); //changed this to -i //might be a division by zero, you should check for that } double function4(int j, int i) { bool invert = false; if(i<0) { i=-i; invert=true; } double result=1; if(i == 0) result = 1; else if(j == 0) result = j; else if (j != 1) { while(i--) result *= j; } return (invert ? 1/result : result); } double function5(int j, int i) { return std::pow(double(j), double(i)); } void test(const char* name, std::function<double(int, int)> f) { for(int j=-3; j<=3; ++j) { std::cout << name << ' '; for(int i=-3; i<=3; ++i) { double t = f(i, j); std::cout <<std::setw(2)<<i<<','<<std::setw(2)<<j<<'='<<std::setw(6)<<std::setprecision(2)<<t<<' '; } std::cout << '\n'; } std::cout << '\n'; } int main() { std::cout << std::fixed; test("function0", function0); test("function4", function4); test("function5", function5); } |
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KCgpkb3VibGUgZnVuY3Rpb24wKGludCBqLCBpbnQgaSkgewogICAgaWYoaSA9PSAwIHx8IGogPT0gMSkgcmV0dXJuIDE7CiAgICBpZihpID09IDEgfHwgaiA9PSAwKSByZXR1cm4gajsKICAgIGlmKGkgPiAwKSByZXR1cm4gaiAqIGZ1bmN0aW9uMChqLCAtLWkpOwogICAgcmV0dXJuIDEgLyAoZnVuY3Rpb24wKGosIC1pKSk7IC8vY2hhbmdlZCB0aGlzIHRvIC1pCiAgICAgLy9taWdodCBiZSBhIGRpdmlzaW9uIGJ5IHplcm8sIHlvdSBzaG91bGQgY2hlY2sgZm9yIHRoYXQKfQoKZG91YmxlIGZ1bmN0a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N0aW9uMCk7CiAgICB0ZXN0KCJmdW5jdGlvbjQiLCBmdW5jdGlvbjQpOwogICAgdGVzdCgiZnVuY3Rpb241IiwgZnVuY3Rpb241KTsKfQ==
-
upload with new input
-
result: Success time: 0s memory: 2888 kB returned value: 0
function0 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00 0,-3= 0.00 1,-3= 1.00 2,-3= 0.12 3,-3= 0.04 function0 -3,-2= 0.11 -2,-2= 0.25 -1,-2= 1.00 0,-2= 0.00 1,-2= 1.00 2,-2= 0.25 3,-2= 0.11 function0 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00 0,-1= 0.00 1,-1= 1.00 2,-1= 0.50 3,-1= 0.33 function0 -3, 0= 1.00 -2, 0= 1.00 -1, 0= 1.00 0, 0= 1.00 1, 0= 1.00 2, 0= 1.00 3, 0= 1.00 function0 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00 0, 1= 0.00 1, 1= 1.00 2, 1= 2.00 3, 1= 3.00 function0 -3, 2= 9.00 -2, 2= 4.00 -1, 2= 1.00 0, 2= 0.00 1, 2= 1.00 2, 2= 4.00 3, 2= 9.00 function0 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00 0, 3= 0.00 1, 3= 1.00 2, 3= 8.00 3, 3= 27.00 function4 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00 0,-3= inf 1,-3= 1.00 2,-3= 0.12 3,-3= 0.04 function4 -3,-2= 0.11 -2,-2= 0.25 -1,-2= 1.00 0,-2= inf 1,-2= 1.00 2,-2= 0.25 3,-2= 0.11 function4 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00 0,-1= inf 1,-1= 1.00 2,-1= 0.50 3,-1= 0.33 function4 -3, 0= 1.00 -2, 0= 1.00 -1, 0= 1.00 0, 0= 1.00 1, 0= 1.00 2, 0= 1.00 3, 0= 1.00 function4 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00 0, 1= 0.00 1, 1= 1.00 2, 1= 2.00 3, 1= 3.00 function4 -3, 2= 9.00 -2, 2= 4.00 -1, 2= 1.00 0, 2= 0.00 1, 2= 1.00 2, 2= 4.00 3, 2= 9.00 function4 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00 0, 3= 0.00 1, 3= 1.00 2, 3= 8.00 3, 3= 27.00 function5 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00 0,-3= inf 1,-3= 1.00 2,-3= 0.12 3,-3= 0.04 function5 -3,-2= 0.11 -2,-2= 0.25 -1,-2= 1.00 0,-2= inf 1,-2= 1.00 2,-2= 0.25 3,-2= 0.11 function5 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00 0,-1= inf 1,-1= 1.00 2,-1= 0.50 3,-1= 0.33 function5 -3, 0= 1.00 -2, 0= 1.00 -1, 0= 1.00 0, 0= 1.00 1, 0= 1.00 2, 0= 1.00 3, 0= 1.00 function5 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00 0, 1= 0.00 1, 1= 1.00 2, 1= 2.00 3, 1= 3.00 function5 -3, 2= 9.00 -2, 2= 4.00 -1, 2= 1.00 0, 2= 0.00 1, 2= 1.00 2, 2= 4.00 3, 2= 9.00 function5 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00 0, 3= 0.00 1, 3= 1.00 2, 3= 8.00 3, 3= 27.00


