69. 콘솔에서 입력규정에 맞게 정수를 입력받고 출력하라
입력 규정:
1Line에서 정수 한개가 입력된다. ( 1 ~ )
2Line에서 공백을 두고 1Line의 정수의 갯수 만큼 정수가 입력된다.
입력 범위 : -2147483648 ~ 2147483647
입력 예시
5
1 2 3 4 5
출력 예시
1
2
3
4
5
JAVA
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
int [] numbers = new int[count];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan.nextInt();
}
scan.close();
for (int number : numbers) {
System.out.println(number);
}
}
}
Swfit 4.2
import Foundation
let firstLine = readLine()
if firstLine != nil, let count = Int(firstLine!) {
let secondLine = readLine()
if secondLine != nil {
var values = [Int]()
for sub in secondLine!.split(separator: " ") {
if let temp = Int(sub) { values.append(temp) }
}
if values.count == count {
for value in values {
print(value)
}
} else {
print("ERROR : values.count")
}
}
}
이번 문제도 저번문제와 마찬가지로 goto문 사용에 대한 문제였습니다만
이를 제맘대로 배열의 갯수를 입력받아 배열만들기로 변경하고 풀었습니다.
이렇게 했더니 이전에 있던 입출력하는 문제랑 별 다르지 않네요;;
https://codeup.kr/problem.php?id=1072
반응형
'iOS > CodeUP - 기초100 With Swift' 카테고리의 다른 글
1074 : [기초-반복실행구조] 정수 한 개 입력받아 카운트다운 출력하기1 with Swift (0) | 2021.05.21 |
---|---|
1073 : [기초-반복실행구조] 0 입력될 때까지 무한 출력하기2 with Swift (0) | 2021.05.21 |
1071 : [기초-반복실행구조] 0 입력될 때까지 무한 출력하기1 with Swift (0) | 2021.05.18 |
1070 : [기초-조건/선택실행구조] 달 입력 받아 계절 출력하기 with Swift (0) | 2021.05.18 |
1069 : [기초-조건/선택실행구조] 평가 입력받아 다르게 출력하기 with Swift (0) | 2021.05.18 |