๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Codeforces] 1167A: Telephone Number

by ์ฃผ๋ฐœ2 2020. 3. 26.
๋ฐ˜์‘ํ˜•

https://codeforces.com/problemset/problem/1167/A

 

Problem - 1167A - Codeforces

 

codeforces.com

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int t = scan.nextInt();
		for(int tc=0; tc<t; tc++) {
			int n = scan.nextInt();	// ๋ฌธ์ž์—ด ๊ธธ์ด
			String s = scan.next();	// ๋ฌธ์ž์—ด
			StringBuilder sb = new StringBuilder(s);
			String result = "";
			while(true) {
				// ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 8๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 11๋ณด๋‹ค ์ž‘์•„์งˆ๊ฒฝ์šฐ ๋ฉˆ์ถ˜๋‹ค.
				if(sb.charAt(0) == '8' || sb.length() < 11)	
					break;
				// 8๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š”๊ฒฝ์šฐ ๋ฌธ์ž ์ œ๊ฑฐ
				else	
					sb.delete(0, 1);	
			}

			// ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 8๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 11์ด์ƒ์ด๋ฉด telephone number์ž„.
			result = (sb.length()>=11) ? "YES" : "NO";
			System.out.println(result);
		}

		scan.close();
	}

}

ํ’€์ด

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ , Telephone Number๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ.

Telephone Number 

 1) ๋ฌธ์ž์—ด์ด '8'๋กœ ์‹œ์ž‘ํ•˜๊ณ 

 2) ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 11์ธ ๊ฒฝ์šฐ

 

๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ '8'์ด ๋‚˜์˜ฌ๋•Œ ๊นŒ์ง€ ๊ณ„์† ์ œ๊ฑฐํ•˜๊ณ , ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 11๋ณด๋‹ค ํฐ์ง€ ์ž‘์€์ง€ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.

 

 

  • ๋ฌธ์ž์—ด์˜ ์‚ฝ์ž…&์‚ญ์ œ๊ฐ€ ์šฉ์ดํ•œ StringBuilder ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ '8'์ด ์•„๋‹Œ๊ฒฝ์šฐ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ '8'์ด๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 11๋ณด๋‹ค ์ž‘์€๊ฒฝ์šฐ -> While ํƒˆ์ถœ.
  • While๋ฌธ์„ ํƒˆ์ถœํ–ˆ์„๋•Œ, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 11๋ณด๋‹ค ํฌ๋ฉด "YES", 11๋ณด๋‹ค์ž‘์œผ๋ฉด "NO"
  • (์•„๋ฌด ๋ฌธ์ž๋‚˜ ์ œ๊ฑฐํ•ด์„œ ๋ฌธ์ž์—ด๊ธธ์ด๋ฅผ 11๋กœ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค)

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€