[Unrated] Lunacy - 4714
4714번: Lunacy
After several months struggling with a diet, Jack has become obsessed with the idea of weighing less. In an odd way, he finds it very comforting to think that, if he had simply had the luck to be born on a different planet, his weight could be considerably
www.acmicpc.net
성능 요약
메모리: 17880 KB, 시간: 220 ms
분류
수학, 구현, 사칙연산
문제 설명
After several months struggling with a diet, Jack has become obsessed with the idea of weighing less. In an odd way, he finds it very comforting to think that, if he had simply had the luck to be born on a different planet, his weight could be considerably less.
Of course, the planets are far out of reach, but even the Earth’s moon would yield a dramatic weight loss. Objects on the moon weight only 0.167 of their weight on Earth.
입력
Input consists of one or more lines, each containing a single floating point number denoting a weight (in pounds) on the Earth. The end of input is denoted by a negative floating point number.
출력
For each line of input data, your program should print a single line of the form
Objects weighing X on Earth will weigh Y on the moon.
where X is the weight from the input and Y is the corresponding weight on the moon. Both output numbers should be printed to a precision of 2 digits after the decimal point.
답 : 지구에서의 물체의 무게를 입력받아, 달에서의 무게로 변환하는 문제이다.
지구의 무게를 입력하다가 음수가 입력되면 입력을 종료한다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double weightOnEarth = 0; // 지구에서의 무게를 저장할 변수
while (true) { // 무한 반복문
weightOnEarth = sc.nextDouble(); // 지구에서의 무게 입력받기
if (weightOnEarth < 0) { // 입력이 음수이면 반복문 탈출
break;
}
double weightOnMoon = weightOnEarth * 0.167; // 달에서의 무게 계산
// 출력문, %.2f는 소수점 둘째자리까지 출력하도록 하는 포맷 문자열
System.out.printf("Objects weighing %.2f on Earth will weigh %.2f on the moon.\n", weightOnEarth, weightOnMoon);
}
sc.close();
}
}
'algorithm > 백준' 카테고리의 다른 글
[백준] 카드 뽑기 - 16204 Java[자바] (0) | 2023.04.11 |
---|---|
[백준] 감소하는 수 -1038 Java[자바] (0) | 2023.04.09 |
[백준] 나부 함대 데이터 - 9654 Java[자바] (0) | 2023.04.01 |
[백준] 11021. A+B - 7 Java[자바] (0) | 2023.04.01 |
[백준] 큰 수 (BIG) - 14928 Java[자바] (0) | 2023.03.31 |