직각삼각형의 밑변이 x, 높이가 y일 때 남은 변(대각선)의 길이를 구하는 함수를 만들어주세요.
ps. 요즘 대각선계산기 어플이 인기가 있는 걸 보고 한번 직접 만들어보는 것도 좋다고 생각해서 문제를 만들어 올립니다.
131개의 풀이가 있습니다.
1
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.println("수를 입력하시요");
int x=scn.nextInt();
int y=scn.nextInt();
int a=(x*x)+(y*y);
double A=Math.sqrt(a);
System.out.print("대각선의 길이는 "+A);
}
왕초보에용
public class coding { public double aa(int x, int y) { return Math.sqrt(x*x + y*y); }
public static void main(String[] args) {
coding c = new coding();
System.out.println(c.aa(4, 7));
}
}
반성하겠습니다.
static void Main(string[] args)
{
Console.Write("높이 : ");
int Height = Convert.ToInt32(Console.ReadLine());
Console.Write("밑변 : ");
int Base = Convert.ToInt32(Console.ReadLine());
double Result = Math.Sqrt(Math.Pow(Height, 2) + Math.Pow(Base, 2));
Console.WriteLine(Result);
}
from math import sqrt
x, y = input("밑변과 높이를 입력하시오").split()
x, y = int(x), int(y)
diagonal = sqrt(x*x + y*y)
print('대각선의 길이는 %.2f' %diagonal)
import java.util.; import java.lang.; import java.io.; import java.math.;
/ Name of the class has to be "Main" only if the class is public. /
class Ideone {
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner=new Scanner(System.in);
double a = Math.pow(scanner.nextInt(), 2);
double b = Math.pow(scanner.nextInt(), 2);
double d = Math.sqrt(a+b);
System.out.println(d);
}
}
float RightAngledTriangle(float a, float b) { float c = sqrt(a*a + b * b); return c; }
void main() { float c = RightAngledTriangle(3, 4); printf("%f", c); }
import java.util.Scanner;
public class RightTriangle { public static void main(String args[]) { int adjacent, height; Scanner sc = new Scanner(System.in); System.out.println("length of adjacent"); adjacent= sc.nextInt(); System.out.println("length of height"); height = sc.nextInt(); double squareaddresult = Math.pow(adjacent, 2)+ Math.pow(height, 2); double result = Math.sqrt(squareaddresult); System.out.println("the length of hypo:"+result); }
}
C#
static double GetDiagonalLength(double x, double y) => Math.Sqrt(Math.Pow(x, 2.0) + Math.Pow(y, 2.0));
#include<stdio.h>
#include<math.h>
int main(void)
{
double x; //직각 삼각형의 밑변
double y; //직각 삼각형의 높이
double result; //대각선의 길이
printf("x의 값을 기입하시오.");
scanf("%lf", &x);
printf("Y의 값을 기입하시오.");
scanf("%lf", &y);
result = sqrt(pow(x,2) + pow(y,2));
printf("밑변의 길이 %.2f\n", x);
printf("높이의 길이 %.2f\n", y);
printf("대각선의 길이는 %.2f 입니다.\n", result);
return 0;
}
C함수의 math.h라이브러리를 사용하여 제작한 함수 입니다.
x = input("x :")
y = input("y :")
print("길이 :", (int(x)**2+int(y)**2)**0.5) #길이 계산
결과
x :5 y :9 길이 : 10.295630140987
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
scanner.close();
System.out.println(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)));
}
while(True):
choose=int(input("계속하시려면 1을 그만두시려면 2를 눌러 주세요: "))
if choose==1:
x=int(input("밑변를 입력해 주세요: "))
y=int(input("높이를 입력해 주세요: "))
result=(x**2+y**2)**0.5
print("결과는",result,"입니다")
else:
break
x, y = map(int, input("Write X and Y with space").split(' ')); print("대각선의 길이:{}".format(float((x**2)+(y**2))**0.5))
int main(void) { int x, y = 0; int z; printf("밑변과 높이를 입력하시오:"); scanf("%d %d", &x, &y); z = (xx + y * y);/루트를 씌울줄 몰라요*/ printf("남은 변의 길이는 %d입니다.", z); return 0; }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Pythagoras(3, 4);
}
static void Pythagoras(int a, int b) // 대각선의 길이를 구하는 메소드
{
Console.WriteLine(Math.Sqrt(a * a + b * b));
}
}
}
x=input('Enter length: ')
y=input('Enter height: ')
X=float(x)
Y=float(y)
diagonal=X**2+Y**2
print('Diagonal:',diagonal**(1/2))
x=int(input("밑변의 길이:"))
y=int(input("높이의 길이:"))
if x>0 and y>0:
num=(x**2+y**2)**0.5
print("diagonal=%s" %num)
#include <iostream>
#include <math.h>
using namespace std;
class Pythagoras{
private:
int scroll;
int height;
public:
Pythagoras(int scroll, int height){this->height = height; this->scroll= scroll; }
void show() { cout << "대각선의 길이 : " << sqrt(pow(scroll,2) + pow(height,2)) << endl; }
};
int main(){
Pythagoras a = Pythagoras(3,4);
a.show();
}
C++ 입니다. 처음 올려보는 거라 조잡합니다.
def length(x,y):
return(x**2+y**2)
print(length(3,4))
결과가 도출되면 직접 루트를 씌워 주십시오. 코드가 간단한 대신 수동적인 느낌을 살려봤습니다.
import java.util.*;
public class 대각선길이구하기 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
System.out.println((int)Math.sqrt(x*x + y*y));
}
}
import math
def sol(a, b): len_line = math.sqrt(pow(a, 2) + pow(b, 2)) return len_line print(sol(3,4)
x = input('밑변 입력 : ')
y = input('높이 입력 : ')
z = ((float(x) ** 2) + (float(y) ** 2)) ** (1/2)
print('대각선의 길이는 %f 입니다.' % float(z))
Python 작성
void main() { double x, y, diagonal; printf("x = "); scanf_s("%f",&x); printf("y = "); scanf_s("%f", &y);
diagonal = sqrt(x*x + y * y);
printf("diagonal = %f", diagonal);
}
package diagonal;
import java.util.Scanner;
public class diagonal {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the width and height:");
int w=sc.nextInt();
int h=sc.nextInt();
double d=Math.sqrt(w*w + h*h);
System.out.println("The diagonal:"+d);
}
}
완전 초보에요. 하나하나 따라가보며 연습하려구요~
def diagonal():
a=int(input('가로 길이를 입력하세요'))
b=int(input('세로 길이를 입력하세요'))
answer=(a**2+b**2)**0.5
return answer
int main(void) { int x; std::cout <<"밑변의 길이를 입력하세요 : "; std::cin >> x;
int y;
std::cout << "높이를 입력하세요 : ";
std::cin >> y;
int result = sqrt(pow(x, 2) + pow(y, 2));
std::cout << "대각선 길이 : " << result << std::endl;
return 0;
}
x1=float(input("밑변의 길이를 입력하십시오: "))
y1=float(input("높이를 입력하십시오: "))
def Hcal(x,y):
x=x1
y=y1
z2=(x**2)+(y**2)
z=float(pow(z2,(1/2)))
return z
print("빗변의 길이는 "+str(Hcal(x1,y1))+"입니다.")
public static void main(String[] args) { Scanner scn=new Scanner(System.in);
System.out.println("수를 입력하시요");
int x=scn.nextInt();
int y=scn.nextInt();
int a=(x*x)+(y*y);
double A=Math.sqrt(a);
System.out.print("대각선의 길이는 "+A);
}
#include<stdio.h>
#include<math.h>
int main(){
int x,y;
int data;
scanf("%d %d",&x,&y);
data=sqrt(x*x+y*y);
printf("%d",data);
}
package codingdozang;
import java.util.Scanner;
import java.lang.Math;
public class Diagonallength {
public static void main(String[] args) {
Scanner scin=new Scanner(System.in);
System.out.println("x,y를 입력하시오");
float x=scin.nextInt();
float y=scin.nextInt();
float data;
double answer;
data=x*x+y*y;
answer=Math.sqrt(data);
System.out.print("대각선 길이는"+answer);
}
}
import math
def trianglediagonal(x,y):
z = math.sqrt(float(x)**2 + float(y)**2) # ^가 아니다
return print(z)
trianglediagonal(1,1)
package BMI;
import java.util.Scanner;
public class Pythagorean_theorem {
public static void main(String args[]) {
Scanner input1 = new Scanner(System.in);
int n1 = input1.nextInt();
int n2 = input1.nextInt();
System.out.println(Math.sqrt(n1*n1 + n2*n2));
}
}
파이썬입니다.
한줄짜리 코딩 해봤습니다..
print('대각선의 길이는 {}이다'.format((sum(tuple(map(lambda x: float(x)**2, input().split()))))**(0.5)))
import math
a= float(input('x='))
b= float(input('y='))
c= math.sqrt(a**2 + b**2)
print(format(c,'.2f'))
int main(void){ int x,y; int da; scanf("%d %d",&x,&y);
da = sqrt((x*x)+(y*y));
printf("%d",da);
}
import math as m
x = int(input("x의 길이:"))
y = int(input("y의 길이:"))
def getDiagonal() :
d = m.sqrt(x**2 + y**2)
return d
diagonal = getDiagonal()
print("대각선 d의 길이는 %.2f입니다."% diagonal)
import os
import math
def calculate():
a = 0
x = input("밑변 = ")
y = input("높이 = ")
if type(x) != 'int' or type(y) != 'int' or type(x) != 'float' or type(y) != 'float':
print('오류: 계산이 불가능 합니다.")
os.system('pause')
else:
x = float(x)
y = float(y)
a = sqrt(x ^ 2 + y ^ 2)
return a
def diagonal(a, b):
result=(a2 + b2)**0.5
return result
print("x=",end='')
x=int(input ())
print("y=",end='')
y=int(input ())
print ("Diagonal is "+str(diagonal(x,y)))
def diagonal():
x = float(input("밑변의 길이를 입력하세요. : "))
y = float(input("높이를 입력하세요. : "))
return (x ** 2 + y ** 2) ** (1/2)
import math
x = eval(input("밑변을 입력하시오: "))
y = eval(input("높이을 입력하시오: "))
diagonal_1 = (x * x) + (y * y)
diagonal = math.sqrt(diagonal_1)
print("대각선의 길이: {}".format(diagonal))
import java.lang.Math; import java.util.Scanner;
public class triSide { static void hypoCal(float a, float b) { float hypo = (float) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); System.out.printf("빗변의 길이 : %.2f", hypo); }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("밑면 : ");
float a = sc.nextFloat();
System.out.print("높이 : ");
float b = sc.nextFloat();
hypoCal(a, b);
}
}
파이썬입니다.
x = int(input("직각삼각형의 밑변을 입력하시오. : "))
y = int(input("직각삼각형의 높이를 입력하시오. : "))
a = (x**2 + y**2)**0.5
b = round(a, 3)
if len(str(a)) > len(str(b)):
print(f"남은 변의 길이는 약 {b}입니다.")
else:
print(f"남은 변의 길이는 {int(a)}입니다.")
import math
def length():
x = int(input())
y = int(input())
result = math.sqrt(x**2+y**2)
print(result)
package main
import (
"fmt"
"math"
)
func main() {
var a int
var b int
fmt.Scanf("%d %d", &a, &b)
fmt.Printf("%0.2f\n", math.Pow((float64)((a*a)+(b*b)), 0.5))
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << pow((a*a) + (b*b), 0.5) << endl;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int a, b;
string c = Console.ReadLine();
a = Int32.Parse(c.Substring(0,1));
b = Int32.Parse(c.Substring(2));
Console.WriteLine(Math.Pow(Convert.ToDouble((a * a) + (b * b)), 0.5));
}
}
}
Go
package main
import (
"fmt"
"math"
)
func main() {
var x, y float64
var n int
var err error
fmt.Print("밑변과 높이를 입력해주세요: ")
n, err = fmt.Scanf("%f %f\n", &x, &y)
if (n == 2) && (err == nil) && (x > 0) && (y > 0) {
// 정상 입력
fmt.Printf("대각선 길이는 %f입니다.\n", math.Sqrt(math.Pow(x, 2)+math.Pow(y, 2)))
} else {
fmt.Println("잘못된 값을 입력하였습니다.")
}
}
파이썬으로 작성하였습니다.
def diagonal(x,y):
return ((x**2)+(y**2))**(0.5)
try:
base = float(input("직각삼각형의 밑변의 길이를 입력하세요:"))
vertical = float(input("직각삼각형의 높이의 길이를 입력하세요:"))
if base <= 0 or vertical <= 0:
print("길이는 양의 값이어야 합니다.")
else:
print(diagonal(base,vertical))
except:
print("오류입니다. 숫자를 입력해주세요.")
using System;
namespace _61일차_10월01일
{
class MainApp
{
static void Main(string[] args)
{
Console.Write("Width Value : ");
string input_Width = Console.ReadLine();
Console.Write("Height Value : ");
string input_Height = Console.ReadLine();
double Result = Math.Pow(int.Parse(input_Width),2) + Math.Pow(int.Parse(input_Height),2);
Console.WriteLine($"대각선의 길이는 : {Result}");
}
}
}
import math
def diagonal(x,y):
return math.sqrt(pow(x,2)+pow(y,2))
print(diagonal(3,4))
print(diagonal(9,12))
파이썬입니다.
>>> import math
>>> def triangle(x,y):
... return math.sqrt(x**2 + y**2)
아래는 사용 예시입니다.
>>> triangle(3, 4)
5.0
def hypo(b, h):
import math
b = int(input('Enter base: '))
h = int(input('Enter height: '))
hyp = math.sqrt(b ** 2 + h ** 2)
return hyp
length = hypo(4, 3)
print(length)
a=float(input("밑변 입력 : "))
b=float(input("높이 입력 : "))
c=(a*a+b*b)**(0.5)
print("대각선의 길이는 ",round(c,2),"입니다")
def get_diagonal():
a = float(input())
b = float(input())
return (a**2 + b**2)**0.5
print(get_diagonal())
#codingdojing_diagonal
import math
x, y = map(int, input().split())
#pythagorean theorem
print('z:', math.sqrt(x**2 + y**2))
a = float(input("밑변의 길이를 입력하세요 : "))
b = float(input("높이의 길이를 입력하세요 : "))
c = round(((a**2)+(b**2))**0.5, 3)
print(f"대각선의 길이는 {c} 입니다")
def byun():
x = int(input("밑변을 입력하세요: "))
y = int(input("높이를 입력하세요: "))
z = (x**2+y**2)**0.5
print(f"대각선 길이는 {round(z,2)} 입니다")
byun()
static void hypotenuse(int x, int y) {
System.out.println(Math.sqrt(x*x+y*y));
}
public static void main(String[] args) {
hypotenuse(3,4);
}
import math
x= int(input("밑변의 길이는 :"))
y= int(input("높이 길이는 :"))
dialog_line = math.sqrt(x**2+y**2)
print("대각선의 길이는 ",dialog_line,"입니다")
import math
length = float(input("Enter 밑변: "))
height = float(input("Enter 높이: "))
print(math.sqrt(length**2 + height**2))
package com.algorithm.algorithmpractice.dojang;
public class GetLength {
private static double getLength(int a, int b){
double result = Math.pow(a, 2) + Math.pow(b, 2);
result = Math.sqrt(result);
return result;
}
public static void main(String[] args) {
System.out.println("result: " + getLength(4, 4));
System.out.println("expected: " + Math.sqrt(32));
}
}
Python
import math
x = 0
y = 0
result = 0
x = int(input("x값 입력: "))
y = int(input("y값 입력: "))
result = math.sqrt(x**2 + y**2)
print(result)
Java
import java.util.*;
class diagonal {
public static void main(String[] args)
{
int x = 0, y = 0;
double diagonal = 0.0;
System.out.printf("x와 y의 값을 입력해주세요");
Scanner scanner = new Scanner(System.in);
x = scanner.nextInt();
y = scanner.nextInt();
diagonal = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
System.out.printf("%f", diagonal);
}
}
하스켈 처음 써봅니다. 와 악명높은 이유를 알겠네...
-- 직각삼각형 밑변x, 높이y로부터 대각선의 길이 구하기
module Main where
main :: IO ()
main = do
x <- fmap read getLine :: IO Float
y <- fmap read getLine :: IO Float
let diagonal a b = sqrt (a * a + b * b)
print (diagonal x y)
def diag_tri():
a = float(input("직각삼각형의 대각선 길이를 구합니다.\n밑변 입력>> "))
b = float(input("높이 입력>> "))
d = (a*a + b*b)**0.5
return d
print(diag_tri())
파이썬 3.9
import math
x = int(input("enter the base length of the triangle:"))
y = int(input("enter the height of the triangle:"))
diagonal = math.sqrt(x**2 + y**2)
print(diagonal)
python
import numpy as np
def cal_diagonal():
x = 1
y = np.sqrt(3)
dia = np.sqrt(x**2+y**2)
return dia
if __name__== '__main__':
diagonal = cal_diagonal()
print(diagonal)
#include <stdio.h>
#include <math.h>
int res(float x, float y){
float res = sqrt(pow(x, 2) + pow(y, 2));
printf("%.2f\n", res);
}
int main(void){
float x, y;
scanf("%f %f", &x, &y);
res(x, y);
return 0;
}
package free;
import java.util.Scanner;
public class test3 {
public static Scanner sc;
public static int getSquare(int number) {
return number * number;
}
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("x좌표 입력 : ");
int x = sc.nextInt();
sc.nextLine();
System.out.print("y좌표 입력 : ");
int y = sc.nextInt();
System.out.println("남은 변 길이는 : " + Math.sqrt(getSquare(x) + getSquare(y)));
sc.close();
}
}
Math.sqrt() 함수를 자체 구현하려 했는데 어떻게 구현해야할까용