์
์ถ๋ ฅ ์
my_string | result |
---|---|
"hi12392" | [1, 2, 2, 3, 9] |
"p2o4i8gj2" | [2, 2, 4, 8] |
"abcde0" | [0] |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
import java.util.Arrays;
public class Sort {
public static int[] solution(String my_string) {
char ch = 0;
String intStr = "";
for(int i = 0; i<my_string.length(); i++) {
ch = my_string.charAt(i);
if(ch >= 48 && ch <= 57) {
intStr += ch;
}
}
int[] answer = new int[intStr.length()]; // answer ๋ฐฐ์ด ์์ฑ
for(int i = 0; i < intStr.length(); i++) {
answer[i] = Character.getNumericValue(intStr.charAt(i)); // ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ซ์๋ก ๋ณํํ์ฌ ๋ฐฐ์ด์ ์ถ๊ฐ
}
Arrays.sort(answer);
return answer;
}
public static void main(String[] args) {
solution("p2o4i8gj2");
}
}
๋์ ์๊ฐ
char ch = 0;
String intStr = "";
for(int i = 0; i<my_string.length(); i++) {
ch = my_string.charAt(i);
if(ch >= 48 && ch <= 57) {
intStr += ch;
}
}
my_string.charAt(i)
๋ฅผ ์ฌ์ฉํ์ฌ, ๋ฌธ์ ํ์
ch์ ๋ด์, ์์คํค ์ฝ๋
๋ฒํธ 48~57 (์ซ์ 0~9)๋ฅผ ๊ฒ์ถ, ๊ฒ์ถ๋ ์ซ์๋ฅผ intStr ๋ฌธ์์ด๋ก ๋ด๋๋ค.
int[] answer = new int[intStr.length()]; // answer ๋ฐฐ์ด ์์ฑ
for(int i = 0; i < intStr.length(); i++) {
answer[i] = Character.getNumericValue(intStr.charAt(i)); // ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ซ์๋ก ๋ณํํ์ฌ ๋ฐฐ์ด์ ์ถ๊ฐ
}
Arrays.sort(answer);
return answer;
}
int[] answer ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๊ฒ์ถ๋ intStr.length()
๋ก ์ก๊ณ for๋ฌธ์ ๋๋ ค int[] answer์ ๋ด๋๋ค
answer[i] = Character.getNumericValue(intStr.charAt(i));
์์ ๊น์ง ์๊ฐํ๋๋ฐ, ๋ฐฐ์ด๋ answer ์, ๋ฌธ์ํ intStr์ ๋ฃ๋ ๋ฐฉ๋ฒ์ ํ์ฐธ ๊ณ ๋ฏผํ๋ค
chatGPT
์ ๋์์ ์์ฒญ...
Character.getNumericValue()
์ดํ, Java ๋ด๋ถ ํด๋์ค Arrays์ sort() ๋ฉ์๋๋ฅผ ํตํด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
์
์ถ๋ ฅ ์
n | t | result |
---|---|---|
2 | 10 | 2048 |
7 | 15 | 229,376 |
์
์ถ๋ ฅ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class Germ {
public static int solution(int n, int t) {
for(int i = 0; i<t; i++) {
n= n*2;
}
return n;
}
public static void main(String[] args) {
solution(7,15);
}
}
๋์ ์๊ฐ
๋งค๊ฐ๋ณ์ n
, t
๊ฐ ์ฃผ์ด์ง๊ธฐ๋๋ฌธ์ n์ 2๋ฐฐํด์ t๋ฒ ๋ฐ๋ณตํ๋ฉด ๋๊ฒ ๊ตฌ๋! ๋ผ๊ณ ... ํธ๊ธฐ๋กญ๊ฒ ์๊ฐํ๋ค๊ฐ ์ด๋ป๊ฒ ํ๊ฒ์ธ๊ฐ๋ฅผ ๊ณ ๋ฏผํ๋ ๋ฌธ์ ์ธ๊ฑฐ๊ฐ๋ค. for๋ฌธ์ ๋จ์ง ๋ฐ๋ณตํ์์ด๊ณ , ๋ฐ๋ณตํ์ ๋งํผ n์ t๋ฒ ๋งํผ ๋ฐ๋ณตํ๋ฉด ๋์ง ์๋๊ฐ์ ๋ํ ์ง๋ฌธ์ ๊ณ์ ๋์ง๊ฒ ํ๋ ์ด๋ง์ด๋งํ ๋ฌธ์ ....
์
์ถ๋ ฅ ์
box | n | result |
---|---|---|
[1, 1, 1] | 1 | 1 |
[10, 8, 6] | 3 | 12 |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class Dice {
public static int solution(int[] box, int n) {
for(int i = 0 ; i<box.length; i++) {
box[i] = box[i] / n;
}
return box[0] *box[1] *box[2];
}
public static void main(String[] args) {
int[] box = {10,8,6};
solution(box,3);
}
}
๋์ ์๊ฐ
๋ฐ์ค์ ํฌ๊ธฐ ๊ฐ๋ก, ์ธ๋ก, ๋์ด๋ฅผ ๋ชจ์๋ฆฌ ๊ธธ์ด๋ก ๋๋ ๋ชซ์ ๊ฐ๊ฐ ๊ณฑํ๋ฉด ๋ค์ด๊ฐ ์ ์๋ ์ฃผ์ฌ์์ ์ด ๊ฐฏ์๋ฅผ ์ ์ ์๋ค. ๋ญ๊ฐ ์ํ์๊ฐ์ ๋ฐฐ์ ๋ ๋ฐฉ๋ฒ์ ์ง๊ฐ(?)์ ์ผ๋ก ์
์
์ถ๋ ฅ ์
n | numlist | result |
---|---|---|
3 | [4, 5, 6, 7, 8, 9, 10, 11, 12] | [6, 9, 12] |
5 | [1, 9, 3, 10, 13, 5] | [10, 5] |
12 | [2, 100, 120, 600, 12, 12] | [120, 600, 12, 12] |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class Nmultiple {
public static int[] solution(int n, int[] numlist) {
int size = 0;
for(int i = 0; i<numlist.length; i++) {
if(numlist[i] % n == 0) {
size ++;
}
}
int[] answer = new int[size];
int j = 0;
for(int i = 0; i<numlist.length; i++) {
if(numlist[i] % n == 0) {
answer[j] = numlist[i];
j++;
}
}
return answer;
}
public static void main(String[] args) {
int[] numlist = {4, 5, 6, 7, 8, 9, 10, 11, 12};
solution(3,numlist);
}
}
๋์ ์๊ฐ
ํด๋ฆฐํ ์ฝ๋๋ก ๋ค๋ฌ์ด ๋ณด๊ธฐ
public class Nmultiple {
public static int[] solution(int n, int[] numlist) {
int size = 0;
for (int num : numList) {
if (num % n == 0) {
size++;
}
}
int[] answer = new int[size];
int j = 0;
for (int num : numList) {
if (num % n == 0) {
answer[j++] = num;
}
}
return answer;
}
"*"
์ ๋์ด์ ๋๋น๋ฅผ 1์ด๋ผ๊ณ ํ์ ๋, "*"์ ์ด์ฉํด ์ง๊ฐ ์ด๋ฑ๋ณ ์ผ๊ฐํ์ ๊ทธ๋ฆฌ๋ ค๊ณ ํฉ๋๋ค. ์ ์ n ์ด ์ฃผ์ด์ง๋ฉด ๋์ด์ ๋๋น๊ฐ n ์ธ ์ง๊ฐ ์ด๋ฑ๋ณ ์ผ๊ฐํ์ ์ถ๋ ฅํ๋๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ณด์ธ์.์
์ถ๋ ฅ ์
3
์ถ๋ ฅ #1
*
**
***
๋์ ํ์ด
package programmers;
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i<= n; i++) {
for(int j = 0; j<i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
๋์ ์๊ฐ
Java๋ฅผ ์ฒ์ ์
๋ฌธํด์ for๋ฌธ์ ๋ฐฐ์ฐ๋ค๋ณด๋ฉด ์๊ฒ๋๋ ์ผ๋ช
๋ณ์ฐ๊ธฐ...๋ฐฐ์ด์ง ๊ฝค ์๊ฐ์ด ์ง๋ฌ๋๋ฐ,
์ด๋ป๊ฒํ๋ฉด ๋ณ์ ์ฐ์ ์ ์์์ง, ์ด๋์ ๋(?)๋ ๋จธ๋ฆฌ์์ผ๋ก ๊ตฌํ์ด ๋๋๊ฒ ์ ๊ธฐํ๋ค...
์ด์ค for๋ฌธ์ ๋๋ ค, ์ฒ์ for๋ฌธ์ ๋ฐ๋ณตํ์, ๋๋ฒ์งธ for๋ฌธ์ ๋ณ์ ๊ฐ์๋ฅผ ์ฐ๋ ๋ก์ง์ ๊ตฌํ
๊ฐ์ ๋ ํ์ด
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++){
System.out.println("*".repeat(i));
}
}
}