๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][1000][A+B]

๋‰ด์ด NUEY 2023. 8. 13. 17:20
๋ฐ˜์‘ํ˜•

 

 

 

๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int firstNumber = scanner.nextInt();
		int secondNumber = scanner.nextInt();
		
		System.out.print(firstNumber + secondNumber);
	}
}

 

 

 

 

์ด๋ฒˆ์—๋„ ๊ตณ์ด ์ž…์ถœ๋ ฅ์„ ๊ตฌ๋ถ„ํ•ด์„œ ๋‹ค์‹œ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
		
    	Main aPlusB = new Main();
        
		List<Integer> numbersList = new ArrayList<Integer>();
		
		numbersList.add(scanner.nextInt());
		numbersList.add(scanner.nextInt());
		
		if (numbersList.size() == 2) aPlusB.printAddNumbers(numbersList);
	}
	
	public void printAddNumbers(List<Integer> inputNumbers) {
		int sum = 0;
		
		Iterator<Integer> numberIter = inputNumbers.iterator();
		
		while (numberIter.hasNext()) sum += numberIter.next();
		
		System.out.printf("%d", sum);
	}
}

 

๋ช‡๊ฐ€์ง€ ์š”์ ์„ ์ •๋ฆฌํ•ด๋ณด๋ฉด

  • ์ถœ๋ ฅ๋ถ€๋ถ„ ํ•จ์ˆ˜๋ฅผ ๋‚˜๋ˆด์Šต๋‹ˆ๋‹ค. (๊ฐ์ฒดํ™”)
  • Collection ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ’์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. List.size()
  • ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•ด์•ผ ํ•˜๊ธฐ์— for๋ฌธ ๋Œ€์‹   Iterator ๊ฐ์ฒด(Collection๊ฐ์ฒด๋‚ด์˜ ๋ชจ๋“  ๊ฐ’์„ ํ™•์ธ)๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
  • printf()ํ•จ์ˆ˜์—์„œ Integerํƒ€์ž…์„ ์ถœ๋ ฅํ•  ๋•Œ๋Š” %d๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

 

์—ด์‹ฌํžˆ ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ ์–ด๋ณด๋ ค ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅ์€ ํ•˜๋Š”๋ฐ ์ตœ์„ ์ธ ์ง€ ๋ชจ๋ฅด๊ฒ ๊ตฐ์š”ใ…œ

 

 

 

๋ฐ˜์‘ํ˜•