Compile Error
foo.c:1:1: error: unknown type name 'function'; did you mean 'union'? 1 | function findLargestPerimeter(base) { | ^~~~~~~~ | union foo.c: In function 'findLargestPerimeter': foo.c:2:5: error: unknown type name 'let' 2 | let height; | ^~~ foo.c:4:20: error: expected expression before '=' token 4 | if (base % 2 === 0) { | ^ foo.c:9:18: error: 'Math' undeclared (first use in this function) 9 | height = Math.floor(base / 2); | ^~~~ foo.c:9:18: note: each undeclared identifier is reported only once for each function it appears in foo.c:13:41: error: invalid type argument of unary '*' (have 'int') 13 | const hypotenuse = Math.sqrt(height**2 + base**2); | ^~ foo.c:13:51: error: invalid type argument of unary '*' (have 'int') 13 | const hypotenuse = Math.sqrt(height**2 + base**2); | ^~ foo.c:16:9: error: 'Number' undeclared (first use in this function) 16 | if (Number.isInteger(height) && Number.isInteger(hypotenuse)) { | ^~~~~~ foo.c: At top level: foo.c:23:1: error: unknown type name 'function'; did you mean 'union'? 23 | function main() { | ^~~~~~~~ | union foo.c: In function 'main': foo.c:27:10: error: unknown type name 'let' 27 | for (let i = 0; i < T; i++) { | ^~~ foo.c:33:9: error: 'console' undeclared (first use in this function) 33 | console.log(result); | ^~~~~~~
Code
function findLargestPerimeter(base) {
let height;
if (base % 2 === 0) {
// If base is even, one possible solution is to set height = base/2 - 1
height = base / 2 - 1;
} else {
// If base is odd, one possible solution is to set height = base//2
height = Math.floor(base / 2);
}
// Calculate hypotenuse using Pythagorean theorem
const hypotenuse = Math.sqrt(height**2 + base**2);
// Check if all sides are integers
if (Number.isInteger(height) && Number.isInteger(hypotenuse)) {
return Math.floor(height + base + hypotenuse);
} else {
return -1;
}
}
function main() {
// Input the number of test cases
const T = parseInt(prompt("Enter the number of test cases:"));
for (let i = 0; i < T; i++) {
// Input base of the triangle for each test case
const B = parseInt(prompt("Enter the base of the triangle:"));
// Find and print the largest perimeter or -1 if no triangle is possible
const result = findLargestPerimeter(B);
console.log(result);
}
}
// Call the main function
main();
Information
- Submit By
- Type
- Submission
- Problem
- P1027 Right triangle
- Contest
- Brain booster #2
- Language
- C99 (GCC 13.2.0)
- Submit At
- 2024-03-06 16:35:46
- Judged At
- 2024-11-11 03:41:17
- Judged By
- Score
- 0
- Total Time
- 0ms
- Peak Memory
- 0 Bytes