23. 02. 09
1. 문자열을 순서대로 배열하기.
단, 문자와 문자 사이에 띄어쓰기가 들어가야하며 마지막 문자 뒤에는 띄어쓰기가 없어야 함.
풀이 1
public class Ex {
public static void main(String[] args) {
System.out.println(smash("먼데", "a", "b", "c", "d"));
}
public static String smash(String... words) {
String smash = new String();
for (int i = 0; i < words.length; i++){
smash += words[i];
if (i != words.length - 1){
smash += " ";
}
}
return smash;
}
}
풀이 2
public class Ex {
public static void main(String[] args) {
System.out.println(smash("먼데", "a", "b", "c", "d"));
}
public class SmashWords {
public static String smash(String... words) {
return String.join(" ", words);
}
}
}
2023.02.10
2. 주어진 정수를 순서대로 곱하기
풀이 1
public class ex2 {
public static int grow(int[] x){
int result = 1;
for (int i = 0; i < x.length; i++) {
result *= x[i];
}
return result;
}
}
2023.02.12
3. 주어진 수가 짝수라면 8을 곱하고 아니라면 9를 곱한다
풀이 1
public class Sid {
public static int simpleMultiplication(int n) {
int sum;
if (n % 2 == 0){
sum = n*8;
}
else {
sum = n*9;
}
return sum;
}
}
2023.02.13
4. 1~7 / 일요일~월요일 대입
풀이 1
public class DayOfWeek {
public static String getDay(int n) {
switch (n) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default:
return "Wrong, please enter a number between 1 and 7";
}
}
}
2023.02.16
5. 점수 3개를 더한 다음 평균을 내고, 그에 따른 등급 매기기
풀이 1
public class GrassHopper {
public static char getGrade(int s1, int s2, int s3) {
char average = 0;
int score = 0;
int sum = (s1 + s2 + s3) / 3;
if(sum >= 90 && sum<= 100)
average = 'A';
else if (sum >= 80 && sum< 90 )
average = 'B';
else if (sum >= 70 && sum< 80 )
average = 'C';
else if (sum >= 60 && sum< 70 )
average = 'D';
else if (sum >= 0 && sum< 60 )
average = 'F';
return average;
}
}
2023.02.17
6. 배열 안에 있는 숫자를 전부 더하는데 양수일 때만 더함
풀이 1
public class sum {
public static int sum(int[] arr){
int sum = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] > 0)
sum += arr[i];
}
return sum;
}
//출력
public static void main(String[] args) {
int arr[] = {3, 2, -2, 2, 10};
System.out.println(sum(arr));
}
}
2023.02.20
7. 손님 이름과 사장 이름이 같은 경우 헬로 보스, 아닌 경우 헬로 게스트
풀이 1
class Kata {
static String greet(String name, String owner) {
if(name.equals(owner)) {
return "Hello boss";
}
else {
return "Hello guest";
}
}
}
2023.02.22
8. 반환값: 음수
풀이 1
public class Kata {
public static int makeNegative(final int x) {
if(x > 0)
return x *(-1);
else
return x;
}
}
풀이 2
public class Kata {
public static int makeNegative(final int x) {
return (x<=0) ? x : -x;
}
}
2023.02.23
9.7 이상의 숫자가 나와도 1~6 순서대로 반복
1: I love you / 2: a little / 3: a lot / 4: passionately / 5: madly / 6: not at all
풀이 1
public class Sid {
public static String howMuchILoveYou(int nb_petals) {
String s = "";
if (nb_petals == 1 || nb_petals % 6 == 1)
s = "I love you";
else if (nb_petals == 2 || nb_petals % 6 == 2)
s = "a little";
else if (nb_petals == 3 || nb_petals % 6 == 3)
s = "a lot";
else if (nb_petals == 4 || nb_petals % 6 == 4)
s = "passionately";
else if (nb_petals == 5 || nb_petals % 6 == 5)
s = "madly";
else if (nb_petals == 6 || nb_petals % 6 == 0)
s = "not at all";
return s;
}
}
풀이 2
public class Sid {
public static String howMuchILoveYou(int nb_petals) {
String[] arr ={"not at all", "I love you", "a little", "a lot", "passionately", "madly"};
return arr[nb_petals%6];
}
}
'문제 풀이 > 자바' 카테고리의 다른 글
자바 문제 풀이 5 (0) | 2023.04.13 |
---|---|
자바 문제 풀이 4 (0) | 2023.04.11 |
자바 문제 풀이 3 (0) | 2023.04.09 |
자바 문제 풀이 2 (0) | 2023.04.08 |
자바 문제 풀이 (0) | 2023.04.05 |
23. 02. 09
1. 문자열을 순서대로 배열하기.
단, 문자와 문자 사이에 띄어쓰기가 들어가야하며 마지막 문자 뒤에는 띄어쓰기가 없어야 함.
풀이 1
public class Ex {
public static void main(String[] args) {
System.out.println(smash("먼데", "a", "b", "c", "d"));
}
public static String smash(String... words) {
String smash = new String();
for (int i = 0; i < words.length; i++){
smash += words[i];
if (i != words.length - 1){
smash += " ";
}
}
return smash;
}
}
풀이 2
public class Ex {
public static void main(String[] args) {
System.out.println(smash("먼데", "a", "b", "c", "d"));
}
public class SmashWords {
public static String smash(String... words) {
return String.join(" ", words);
}
}
}
2023.02.10
2. 주어진 정수를 순서대로 곱하기
풀이 1
public class ex2 {
public static int grow(int[] x){
int result = 1;
for (int i = 0; i < x.length; i++) {
result *= x[i];
}
return result;
}
}
2023.02.12
3. 주어진 수가 짝수라면 8을 곱하고 아니라면 9를 곱한다
풀이 1
public class Sid {
public static int simpleMultiplication(int n) {
int sum;
if (n % 2 == 0){
sum = n*8;
}
else {
sum = n*9;
}
return sum;
}
}
2023.02.13
4. 1~7 / 일요일~월요일 대입
풀이 1
public class DayOfWeek {
public static String getDay(int n) {
switch (n) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default:
return "Wrong, please enter a number between 1 and 7";
}
}
}
2023.02.16
5. 점수 3개를 더한 다음 평균을 내고, 그에 따른 등급 매기기
풀이 1
public class GrassHopper {
public static char getGrade(int s1, int s2, int s3) {
char average = 0;
int score = 0;
int sum = (s1 + s2 + s3) / 3;
if(sum >= 90 && sum<= 100)
average = 'A';
else if (sum >= 80 && sum< 90 )
average = 'B';
else if (sum >= 70 && sum< 80 )
average = 'C';
else if (sum >= 60 && sum< 70 )
average = 'D';
else if (sum >= 0 && sum< 60 )
average = 'F';
return average;
}
}
2023.02.17
6. 배열 안에 있는 숫자를 전부 더하는데 양수일 때만 더함
풀이 1
public class sum {
public static int sum(int[] arr){
int sum = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] > 0)
sum += arr[i];
}
return sum;
}
//출력
public static void main(String[] args) {
int arr[] = {3, 2, -2, 2, 10};
System.out.println(sum(arr));
}
}
2023.02.20
7. 손님 이름과 사장 이름이 같은 경우 헬로 보스, 아닌 경우 헬로 게스트
풀이 1
class Kata {
static String greet(String name, String owner) {
if(name.equals(owner)) {
return "Hello boss";
}
else {
return "Hello guest";
}
}
}
2023.02.22
8. 반환값: 음수
풀이 1
public class Kata {
public static int makeNegative(final int x) {
if(x > 0)
return x *(-1);
else
return x;
}
}
풀이 2
public class Kata {
public static int makeNegative(final int x) {
return (x<=0) ? x : -x;
}
}
2023.02.23
9.7 이상의 숫자가 나와도 1~6 순서대로 반복
1: I love you / 2: a little / 3: a lot / 4: passionately / 5: madly / 6: not at all
풀이 1
public class Sid {
public static String howMuchILoveYou(int nb_petals) {
String s = "";
if (nb_petals == 1 || nb_petals % 6 == 1)
s = "I love you";
else if (nb_petals == 2 || nb_petals % 6 == 2)
s = "a little";
else if (nb_petals == 3 || nb_petals % 6 == 3)
s = "a lot";
else if (nb_petals == 4 || nb_petals % 6 == 4)
s = "passionately";
else if (nb_petals == 5 || nb_petals % 6 == 5)
s = "madly";
else if (nb_petals == 6 || nb_petals % 6 == 0)
s = "not at all";
return s;
}
}
풀이 2
public class Sid {
public static String howMuchILoveYou(int nb_petals) {
String[] arr ={"not at all", "I love you", "a little", "a lot", "passionately", "madly"};
return arr[nb_petals%6];
}
}
'문제 풀이 > 자바' 카테고리의 다른 글
자바 문제 풀이 5 (0) | 2023.04.13 |
---|---|
자바 문제 풀이 4 (0) | 2023.04.11 |
자바 문제 풀이 3 (0) | 2023.04.09 |
자바 문제 풀이 2 (0) | 2023.04.08 |
자바 문제 풀이 (0) | 2023.04.05 |