๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/25304

 

์œ„์—๋Š” ๋ฌธ์ œ ๋งํฌ!

์•„๋ž˜๋Š” ๋ฌธ์ œ ์บก์ณ!

 

 


 

Java ๋กœ ํ’€์—ˆ๋‹ค!

๋‚˜๋งŒ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ๋‚˜ ใ…Ž ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์€ ์—„์ฒญ ์‰ฝ๊ฒŒ ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค.

๋‚˜๋Š” ์ค‘๊ฐ„์ค‘๊ฐ„ ๋ฌธ์žฅ๋„ ๋„ฃ์—ˆ๋‹ค

 

import java.util.Scanner;

public class cal {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner input = new Scanner(System.in);
		System.out.println("Your total price : ");
		int total = input.nextInt();

		System.out.println("How many kind of things did you buy? : ");
		int kind = input.nextInt();
		
		//์œ„์—์„œ ๋‚˜๋Š” 4ํ•˜๊ณ  enter ๋ฅผ ์น˜๋ฉด, enter ("\n") ๊ฐ€ ๊ทธ ๋‹ค์Œ input.nextLine() ๋ฉ”์„œ๋“œ๋Š” ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๋Š” ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž \n์„ ์ฝ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋นˆ ๋ฌธ์ž์—ด ""์ด abstr[0]์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
		// ๊ทธ๊ฑธ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ถ€๋ถ„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚จ์€ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ์†Œ๋น„
		input.nextLine();
		
		System.out.println("Then let me know all of the price and the EA" );
		String[] abstr = new String[kind];
		
		for (int i = 0; i<kind; i++) {
			abstr[i] = input.nextLine();
			String[][] absplitstr = {abstr[i].split("\\s")};
			int[] absplitint = {Integer.parseInt(absplitstr[0][0]), Integer.parseInt(absplitstr[0][1])};
			total -= (absplitint[0]*absplitint[1]);
		}
		
		if (total == 0) {
			System.out.println("Yes!");
		} else {
			System.out.println("no!");
		}

		
	}

 

 

์ฐธ๊ณ ๋กœ, ๋‚ด๊ฐ€ 17๋ฒˆ line ์˜ ์ฝ”๋“œ๋ฅผ ๋นผ๋จน์—ˆ์„ ๋•Œ, abstr[0] = ""; ์ด๋ ‡๊ฒŒ ๋นˆ์นธ์ด ๋“ค์–ด๊ฐ€๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๊ณ , ๊ทธ ์ด์œ ๋ฅผ ๋ชฐ๋ž๋Š”๋ฐ, Chat gpt ์„ ์ƒ๋‹˜์ด ์นœ์ ˆํ•˜๊ฒŒ ๊ทธ ์ด์œ ๋ฅผ ์•Œ๋ ค์คฌ๋‹ค.

์•„๋ž˜๋Š” gpt ๋‹ต๋ณ€!

 


abstr[0]์— ๋นˆ ๋ฌธ์ž์—ด ""์ด ๋“ค์–ด๊ฐ€๋Š” ์ด์œ ๋Š” Scanner ๊ฐ์ฒด์˜ nextLine() ๋ฉ”์„œ๋“œ๊ฐ€ ์ด์ „ ์ž…๋ ฅ์—์„œ ๋‚จ์€ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž(\n)๋ฅผ ์ฝ์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์•„๋ž˜์—์„œ ์ด ์ƒํ™ฉ์„ ๋‹จ๊ณ„๋ณ„๋กœ ์„ค๋ช…ํ• ๊ฒŒ์š”.

์ฝ”๋“œ ๋™์ž‘ ๊ณผ์ •

  1. total ๊ฐ’ ์ž…๋ ฅ: ์‚ฌ์šฉ์ž๊ฐ€ input.nextInt()๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ 260000์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’ ๋’ค์— Enter ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด, ์ˆซ์ž 260000 ๋’ค์— \n (์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž)์ด ์ž…๋ ฅ ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  2. kind ๊ฐ’ ์ž…๋ ฅ: ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, input.nextInt()๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ 4๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  \n์ด ๋‹ค์‹œ ์ž…๋ ฅ ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. ์ฒซ ๋ฒˆ์งธ nextLine() ํ˜ธ์ถœ: ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ abstr[0] = input.nextLine();์ด ์‹คํ–‰๋˜๋ฉด, input.nextLine() ๋ฉ”์„œ๋“œ๋Š” ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๋Š” ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž \n์„ ์ฝ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋นˆ ๋ฌธ์ž์—ด ""์ด abstr[0]์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  4. ์ดํ›„ nextLine() ํ˜ธ์ถœ: ๊ทธ ๋‹ค์Œ ๋ฐ˜๋ณต์—์„œ input.nextLine()์ด ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ œ๋Œ€๋กœ ์ฝ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด nextInt() ๋‹ค์Œ์— nextLine()์„ ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•˜์—ฌ ์ž…๋ ฅ ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๋Š” ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ์†Œ๋น„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


 

 

์ด์ œ ์•ž์œผ๋กœ ์Šต๊ด€์„ ๋“ค์ด์ž!

๊ผญ! nextInt(); ๋ฅผ ์“ฐ๋ฉด, ์•„๋ž˜์— input.nextLine(); ์„ ์จ์ค˜์„œ "\n" ์„ ์†Œ๋น„ํ•ด์ฃผ์ž!

 

int num = input.nextInt();

input.nextLine();

 

 

 

๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ํ’€์—ˆ๋‚˜? ์‹ถ์€ ๋ฐฑ์ค€ ๋ฌธ์ œ java ํ’€์ด ๋!

๋ฐ˜์‘ํ˜•

+ Recent posts