[Java] Solution to puzzles

Joy🌱·2022λ…„ 12μ›” 25일
0

🧩 Coding Challenges

λͺ©λ‘ 보기
4/20
post-thumbnail
post-custom-banner

🧩 μ •μˆ˜ ν•œ 개λ₯Ό μž…λ ₯λ°›κ³ , 1λΆ€ν„° μž…λ ₯받은 μ •μˆ˜κΉŒμ§€μ˜ 합을 κ³„μ‚°ν•˜κΈ°

πŸ’β€ μ •μˆ˜ ν•œ 개λ₯Ό μž…λ ₯λ°›κ³ , 1λΆ€ν„° μž…λ ₯받은 μ •μˆ˜κΉŒμ§€μ˜ 합을 κ³„μ‚°ν•΄μ„œ 좜λ ₯ν•˜μ„Έμš”.

import java.util.Scanner;

public class Application2 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("μ •μˆ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ");
		int num = sc.nextInt();
		
		int result = 0;
		
		for(int i = 1; i <= num; i++) {
			result += i;
		}
		System.out.println("1λΆ€ν„° " + num + "κΉŒμ§€μ˜ ν•© : " + result);
	}
}

🧩 1λΆ€ν„° μž…λ ₯받은 μ •μˆ˜κΉŒμ§€μ˜ 짝수의 합을 κ΅¬ν•˜κΈ°

πŸ’β€ 1λΆ€ν„° μž…λ ₯받은 μ •μˆ˜κΉŒμ§€μ˜ 짝수의 합을 κ΅¬ν•˜μ„Έμš”.

import java.util.Scanner;

public class Application3 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("μ •μˆ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ");
		int num = sc.nextInt();
		
		int result = 0;
		
		for(int i = 1; i <= num; i++) {
			if(i % 2 == 0) {
			result += i;
			}
		}
		System.out.println("1λΆ€ν„° " + num + "κΉŒμ§€ 짝수의 ν•© : " + result);
	}
}

🧩 λ¬Έμžμ—΄μ„ μž…λ ₯ λ°›μ•„ λ¬Έμžμ—΄μ˜ 각 μΈλ±μŠ€λ³„λ‘œ ν•œ κΈ€μžμ”© 좜λ ₯ν•˜κΈ°

πŸ’β€ λ¬Έμžμ—΄μ„ μž…λ ₯ λ°›μ•„μ„œ λ¬Έμžμ—΄μ˜ 각 μΈλ±μŠ€λ³„λ‘œ ν•œ κΈ€μžμ”© 좜λ ₯ν•˜μ„Έμš”.

🚩 Example Input & Output

λ¬Έμžμ—΄μ„ μž…λ ₯ν•˜μ„Έμš” : 
Hyoyeon
0 : H
1 : y
2 : o
3 : y
4 : e
5 : o
6 : n
import java.util.Scanner;

public class Application1 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("λ¬Έμžμ—΄μ„ μž…λ ₯ν•˜μ„Έμš” : ");
		String word = sc.nextLine();
		
		for(int i = 0; i <= word.length(); i++ )
			System.out.println(i + " : " + word.charAt(i));
	}
}

πŸ’¬ Overall Comment

* μ²˜μŒμ—λŠ” 아이디어가 μ—†μ–΄μ„œ κ°ˆν”Όλ₯Ό λͺ» μž‘μ•˜μ§€λ§Œ μ—¬λŸ¬ ν‚€μ›Œλ“œλ₯Ό κ²€μƒ‰ν•˜μ—¬ μž…λ ₯받은 λ¬Έμžμ—΄μ˜ 길이(.length)λ₯Ό 
  κ΅¬ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό μ°Ύμ•„λ‚΄μ—ˆλ‹€. iλŠ” μž…λ ₯ν•œ λ¬Έμžμ—΄μ˜ 길이만큼 μ¦κ°€ν•˜κ³  μž…λ ₯ν•œ λ¬Έμžμ—΄μ˜ μžλ¦Ώκ°’μ„ 좜λ ₯(.charAt)ν•΄μ£ΌλŠ” 
  λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 닡을 κ΅¬ν–ˆλ‹€. 

🧩 for문을 μ΄μš©ν•˜μ—¬ μ•ŒνŒŒλ²³ μ†Œλ¬Έμž 'a'λΆ€ν„° 'z'κΉŒμ§€λ₯Ό κ°œν–‰ 없이 μ°¨λ‘€λ‘œ 좜λ ₯ν•˜κΈ°

πŸ’β€ λ°˜λ³΅λ¬Έμ„ μ΄μš©ν•˜μ—¬ μ•ŒνŒŒλ²³ μ†Œλ¬Έμž 'a'λΆ€ν„° 'z'κΉŒμ§€λ₯Ό κ°œν–‰ 없이 μ°¨λ‘€λ‘œ 좜λ ₯ν•˜μ„Έμš”.

public class Application2 {
	
	public static void main(String[] args) {
		
		for(int i = 97; i <= 122; i++ ) {
			System.out.print((char)i);
		}
	}
}

🧩 μ •μˆ˜λ₯Ό μž…λ ₯λ°›μ•„ ν™€μˆ˜ 및 μ§μˆ˜μ— 따라 κ°’ λ‹€λ₯΄κ²Œ 좜λ ₯ν•˜κΈ°

πŸ’β€ μ •μˆ˜λ₯Ό μž…λ ₯λ°›μ•„ 1λΆ€ν„° μž…λ ₯받은 μ •μˆ˜κΉŒμ§€ ν™€μˆ˜μ΄λ©΄ "수", 짝수이면 "λ°•"이 μ •μˆ˜λ§ŒνΌ λˆ„μ λ˜μ–΄ 좜λ ₯되게 μž‘μ„±ν•˜μ„Έμš”.

import java.util.Scanner;

public class Application3 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("μ •μˆ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ");
		int num = sc.nextInt();
		
		for(int i = 1; i <= num; i++) {
			if(i % 2 == 1) {
				System.out.print("수");
			} else {
				System.out.print("λ°•");
			}
		}
	}
}

profile
Tiny little habits make me
post-custom-banner

0개의 λŒ“κΈ€