import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int value0 = scan.nextInt();
scan.close();
long printValue = (long) value0;
System.out.println(++printValue);
}
}
Swift 4.2
import Foundation
extension Int {
static prefix func ++(_ x: inout Int) -> Int {
x += 1
return x
}
}
let line = readLine()
if line != nil {
if var value = Int(line!) {
print(++value)
}
}
이번 문제는 증감연산자를 사용해보는게 목적인 문제인듯하여
굳이 ++을 사용해봤습니다.
switf 4.2에선 해당 연사자가 디폴트로 존재하지 않기때문에 전위 증감 연산자로 Int에 한해서 사용하도록 추가해봤습니다.
1046 : [기초-산술연산] 정수 세 개 입력받아 합과 평균 출력하기 with Swift
43.콘솔에서 세 개의 정수를 입력받아 합과 평균을 출력하라.
입력 : 1line으로 공백으로 구분되어 3개의 정수가 입력됨 (입력 범위 : -2147483648 ~ -2147483647)
출력:
1line : 세 정수의 합
2line : 세 정수의 평균( 실수, 소수점 둘째 자리에서 반올림하여 첫째 자리까지 출력 )
입력 예시
1 2 3
출력 예시
6
2.0
JAVA
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [3];
for (int i = 0; i < values.length; i++) {
values[i] = scan.nextInt();
}
scan.close();
if(values.length > 2) {
long total = values[0] + values[1] + values[2];
double avg = total / 3.0;
System.out.println(total);
System.out.println(String.format("%.1f", avg));
}
}
}
Swift 4.2
import Foundation
let line = readLine()
if line != nil {
let split = line!.split(separator: " ")
if split.count == 3 {
var values = [Int]()
for s in split {
if let value = Int(s) {
values.append(value)
}
}
if values.count == 3 {
let total = values[0] + values[1] + values[2]
let avg = Float(total) / 3.0
print(total)
print(String.init(format: "%.1f", avg))
}
}
}
뭐.. 이런곳에서는 의미는 없습니다만
보통 배열이라던가 반복이라던가 할때 직접적으로 숫자를 써서 위치를 입력받는건 위험한 일입니다.
1041 : [기초-산술연산] 문자 한 개 입력받아 다음 문자 출력하기 with Swift
38. 콘솔에서 영문자 한 개를 입력받아, 입력된 문자의 아스키코드값의 다음에 해당하는 문자를 출력하라.
입력 예시
a
출력 예시
b
JAVA
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
scan.close();
char [] charArray = line.toCharArray();
if(charArray.length > 0) {
System.out.println((char) (charArray[0] + 1));
}
}
}
Swift 4.2
import Foundation
let line = readLine()
if line != nil {
if let printValue = line!.first {
let unicodes = printValue.unicodeScalars
for uni in unicodes {
if let printValue = Unicode.Scalar(uni.value + 1) {
print(printValue)
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int value0 = scan.nextInt();
int value1 = scan.nextInt();
scan.close();
if(value1 > 0) {
System.out.println(value0/value1);
}
}
}
Swift 4.2
import Foundation
let line = readLine()
if line != nil {
let array = line!.split(separator: " ")
if array.count == 2 {
if let value0 = Int(array[0]), let value1 = Int(array[1]) {
if value1 > 0 {
print(value0/value1)
}
}
}
}
1043 : [기초-산술연산] 정수 두 개 입력받아 나눈 나머지 출력하기 with Swift
40. 콘솔에서 정수 두개(a,b)를 입력 받아 a를 b로 나눈 나머지를 출력하라.
입력범위: 0 ~ 2147483647
입력 예시
1 3
출력 예시
1
JAVA
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int value0 = scan.nextInt();
int value1 = scan.nextInt();
scan.close();
if(value1 > 0) {
System.out.println(value0%value1);
}
}
}
Swift 4.2
import Foundation
let line = readLine()
if line != nil {
let array = line!.split(separator: " ")
if array.count == 2 {
if let value0 = Int(array[0]), let value1 = Int(array[1]) {
if value1 > 0 {
print(value0%value1)
}
}
}
}