[input] array.integer sides array of three integers representing triangle sides
[output] boolean true if the triangle with sides from the sides array is a right triangle, false otherwise
bool rightTringle(std::vectorsides){ struct Helper{ int sqr(int value){ return value*value; } }; Helper h; std::sort(sides.gegin(),sides.end()); if(h.sqr(sides[0]+h.sqr(sides[1]==h.sqr(sides[2])){ return true; } return false }
2.If the given integer is greater than 12 return this number, otherwise return 12.
[input] integer number positive integer
[output] integer
int returnTwelve(int number) { while(number < 12){ number = number + 1; } return number }
3.Given two integers a and b, find the remainder of a when divided by b.
Example for a=5, b=3 output should be 2
[input] integer a a positive integer
[input] integer b a positive integer
[output] integer
int findTheRemainder(int a, int b) { while(a > b){ a = a -b; } return a; }
4.Given a string, find the number of different characters in it. for "cabca" output should be 3
[input] string s a string of lowercase latin letters
[output] integer
int differentSymbolsNaive(std::string s) { int result = 0; for (int i = 0; i < 26; i++) { bool found = false; for (int j = 0; j < s.size(); j++) { if (s[j] = 'a' + i) { found = true; break; } } if (found) { result++; } } return result; }
5.Given an integer n, find the volume of the cube with edges equal to n.
Example for n=5 output should be 125
[input] integer n positive integer
[output] integer
int cube(int n){ int result =1; for(int i = 0; i < 3; i++){ result = result * n; } return result; }
沒有留言:
張貼留言