이 문제에는 표준 입력으로 두 개의 정수 n과 m이 주어집니다.
별(*) 문자를 이용해 가로의 길이가 n, 세로의 길이가 m인 직사각형 형태를 출력해보세요.
n과 m은 각각 1000 이하인 자연수입니다.
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = 0; i < b; i++) {
for( int j = 0; j < a; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
색다른 풀이! 자바가 아니라 전혀 다른 언어인 줄 알았다..
import java.util.Scanner;
import java.util.stream.IntStream;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
StringBuilder sb = new StringBuilder();
IntStream.range(0, a).forEach(s -> sb.append("*"));
IntStream.range(0, b).forEach(s -> System.out.println(sb.toString()));
}
}
정수 num이 짝수일 경우 "Even"을 반환하고 홀수인 경우 "Odd"를 반환하는 함수, solution을 완성해주세요.
class Solution {
public String solution(int num) {
return num % 2 == 0 ? "Even" : "Odd";
}
}
==
나 !=
가 아니라 > 0
으로도 표현 할 수 있구나!
class Solution {
public String solution(int num) {
return (Math.abs(num) % 2 > 0) ? "Odd" : "Even";
}
}
단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.
s는 길이가 1 이상, 100이하인 스트링입니다.
class Solution {
public String solution(String s) {
String answer = "";
int idx = s.length() / 2;
if (s.length() % 2 == 0) {
answer = s.substring(idx - 1, idx + 1);
} else {
answer = s.substring(idx, idx + 1);
}
return answer;
}
}
조건 처리고 뭐고 한줄이면 되는 거였구나...
class Solution {
public String solution(String s) {
return word.substring((word.length() - 1) / 2, word.length() / 2 + 1);
}
}
두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.
예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.
class Solution {
public long solution(int a, int b) {
long answer = 0;
if (a >= b) {
int temp = a;
a = b;
b = temp;
}
for (int i = a; i <=b; i++) {
answer += i;
}
return (long) answer;
}
}
이 코드를 보고 바로 현타왔다...
등차수열의 합 공식, 분명 학교다닐때 배웠었던 것 같은데 생각도 못했다. 대단대단
class Solution {
public long solution(int a, int b) {
return sumAtoB(Math.min(a, b), Math.max(b, a));
}
private long sumAtoB(long a, long b) {
return (b - a + 1) * (a + b) / 2;
}
}
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요.
예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다.
str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다.
class Solution {
public int solution(String s) {
boolean isMinus = false;
if (s.contains("-")) {
s = s.replace("-", "");
isMinus = true;
}
int answer = Integer.parseInt(s);
return isMinus ? answer * - 1 : answer;
}
}
부호도 변환 되는줄 몰랐던 1인.. 반성합니다ㅠㅠ
class Solution {
public int solution(String s) {
return Integer.parseInt(s);
}
}