본문 바로가기
학부공부/Java_Basic

Chapter 4. Mathematical Functions, Characters, and Strings[Java Basic]

by sonpang 2021. 11. 5.
반응형

4.A - 기하: 5각형의 넓이

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

오각형의 중심으로부터 정점까지의 거리 r을 입력받아 오각형의 넓이를 계산하는 프로그램을 작성하세요.

Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure.

where r is the length from the center of a pentagon to a vertex. Round up two digits after the decimal point.

 

INPUT

* Line 1 : 실수 r (0~100)

 

OUTPUT

* Line 1 : pentagon의 넓이 (소수점 둘째 자리로 반올림)

 

SAMPLE INPUT

5.5

 

SAMPLE OUTPUT

71.92

 

import java.util.Scanner;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Double r = input.nextDouble();
        Double s = 2 * r * Math.sin(Math.PI / 5);
        Double area = (5 * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / 5));
        System.out.printf("%.2f",area);
    }
}



4.B - 기하: 대권거리

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

대권거리(great circle distance)는 지구표면에서 두 점 사이의 거리를 의미합니다. (x1, y1)과 (x2, y2)를 위도(latitude)와 경도(longitude)로 구성된 두 지점이라고 할때, 두 지점 사이의 대권거리는 다음 공식을 통해서 계산할 수 있습니다.

The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula:

여러분은 위도와 경도로 구성된 두 지점을 입력으로 받아 대권거리를 계산하는 프로그램을 작성해야 합니다. 평균 지구 반경 radius 는 6,371.01 km 이고, 여러분은 degrees를 radians으로 변환하기 위해 Java에서 제공하는 Math.toRadians 를 활용해야 할 수 있습니다. 위 식에서 사용된 위도와 경도는 북서를 기준으로 하기 때문에 음수가 나온다면 남동쪽을 의미합니다.

Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average earth radius is 6,371.01 km. Note that you need to convert the degrees into radians using the Math.toRadians method since the Java trigonometric methods use radians. The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees.

 

INPUT

* Line 1 : x1 y1

* Line 2 : x2 y2

 

OUTPUT

* Line 1 : 두점 사이의 거리를 km단위로 출력 (소수점 둘째자리로 반올림)

 

SAMPLE INPUT

39.55 -116.25

41.5 87.37

 

SAMPLE OUTPUT

10691.79

 

import java.util.Scanner;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Double x1 = Math.toRadians(input.nextDouble()), y1 = Math.toRadians(input.nextDouble()), x2 = Math.toRadians(input.nextDouble()), y2 = Math.toRadians(input.nextDouble());
        final Double radius = 6371.01;
        Double d = radius * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * + Math.cos(y1 - y2));
        System.out.printf("%.2f", d);
    }
}



4.C - 기하: 정다각형의 넓이

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

정다각형은 변의 길이가 모두 동일한 다각형입니다. 변의 개수 n과 변의 길이 s가 주어졌을때 정다각형의 넓이를 계산하는 프로그램을 작성하세요. 정다각형의 넓이를 계산하는 공식은 다음과 같습니다.

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is

Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area.

 

INPUT

* Line 1 : 정수 n (3~100)

* Line 2 : 실수 s (0~100)

 

OUTPUT

* Line 1 : area의 넓이 (소수점 둘째자리까지만 출력, 예: 11.263의 경우 11.26로 출력하고 11.00의 경우 11.00로 출력, -11.256의 경우 -11.25로 출력)

 

SAMPLE INPUT

5

6.5

 

SAMPLE OUTPUT

72.69

import java.util.Scanner;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Double n = input.nextDouble(), s = input.nextDouble();
        Double area = (n * Math.pow(s,2)) / (4 * Math.tan(Math.PI / n));
        System.out.printf("%.2f",Math.floor(area * 100) / 100.0);
    }
}

Wrong Answer
import java.util.Scanner;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Double n = input.nextDouble(), s = input.nextDouble();
        Double area = (n * Math.pow(s,2)) / (4 * Math.tan(Math.PI / n));
        System.out.println(Math.floor(area * 100) / 100.0);
    }
}

System.out.println(Math.floor(area * 100) / 100.00); 이라면?

 

반응형

4.D - 10진수를 16진수로

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

0부터 100사이의 정수를 입력받아 16진수로 출력하는 프로그램을 작성하세요.

Write a program that prompts the user to enter an integer between 0 and 100 and displays its corresponding hex number.

 

INPUT

* Line 1 : 정수 (0~100)

 

OUTPUT

* Line 1 : HEX값 (0~64범위의 hex값; a-f는 소문자로 표시)

 

SAMPLE INPUT

100

 

SAMPLE OUTPUT

64

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println(Integer.toHexString(n).toLowerCase());
    }
}



4.E - 학생의 전공과 학년

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

전공과 학년을 의미하는 두개의 문자를 입력으로 받아 전공과 학년의 완벽한 이름을 출력하는 프로그램을 작성하세요. 첫번째 문자는 전공을 나타내며 아래 보이는 목록중 하나입니다. 두번째 문자는 학년을 나타내며 1부터 차례대로 Freshman, Sophomore, Junior, Senior로 표현하면 됩니다.

Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following chracters are used to denote the majors:

 

M: Mathematics

C: Computer Science

I: Information Technology

 

INPUT

* Line 1 : 문자x2

 

OUTPUT

* Line 1 : 전공과 학년을 공백으로 구분해 출력

 

SAMPLE INPUT

C3

 

SAMPLE OUTPUT

Computer Science Junior

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.next();
        char c = s.charAt(0);
        int n = s.charAt(1) - 48;
        String data[] = {"Freshman", "Sophomore", "Junior", "Senior"};
        String major = "Information Technology";
        if(c == 'M')
            major = "Mathematics";
        else if(c == 'C')
            major = "Computer Science";
        System.out.println(major + " " + data[n - 1]);
    }
}



4.F - 세개의 도시명을 정렬

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

세개의 도시명을 입력받아 오름차순으로 출력하는 프로그램을 작성하세요.

Write a program that prompts the user to enter three cities and displays them in ascending order.

 

INPUT

* Line 1 : 도시1을 나타내는 문자열

* Line 2 : 도시2을 나타내는 문자열

* Line 3 : 도시3을 나타내는 문자열

(각 문자열의 길이는 100을 넘지 않는다)

 

OUTPUT

* Line 1 : 도시명들을 오름차순으로 정렬한후 공백으로 연결시켜 출력

 

SAMPLE INPUT

Chicago

Los Angeles

Atlanta

 

SAMPLE OUTPUT

Atlanta Chicago Los Angeles

 

import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String city[] = new String[3];
        city[0] = input.nextLine();
        city[1] = input.nextLine();
        city[2] = input.nextLine();
        Arrays.sort(city);
 
        System.out.println(city[0] + " " + city[1] + " " + city[2]);
    }
}

 

 

 

반응형

댓글