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

๐Ÿ“• STUDY/Algorithm

[C / C++] ๋ฐฑ์ค€ 5086๋ฒˆ - ๋ฐฐ์ˆ˜์™€ ์•ฝ์ˆ˜

๋ฐฑ์ค€ 1316๋ฒˆ : https://www.acmicpc.net/problem/5086

 

1260๋ฒˆ: DFS์™€ BFS

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ

www.acmicpc.net

โœ… ๋ฌธ์ œ ์„ค๋ช…

0 0์„ ์ž…๋ ฅ(์ข…๋ฃŒ์กฐ๊ฑด)ํ•˜๊ธฐ ์ „๊นŒ์ง€,

์ˆซ์ž ๋‘๊ฐœ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์•ฝ์ˆ˜์ธ์ง€ ๋ฐฐ์ˆ˜์ธ์ง€ ์•„๋ฌด๊ด€๊ณ„๋„ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

[ ์•ฝ์ˆ˜๋ผ๋ฉด factor, ๋ฐฐ์ˆ˜๋ผ๋ฉด multiple, ์•„๋ฌด๊ด€๊ณ„๋„ ์•„๋‹ˆ๋ผ๋ฉด neither๋ฅผ ์ถœ๋ ฅ ]


โœ… ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๋ช…

1. ๋‘ ์ˆ˜ a,b๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

 

2. a๋ฅผ b๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ผ๋ฉด a๊ฐ€ b์˜ ์•ฝ์ˆ˜

3. b๋ฅผ a๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ผ๋ฉด a๊ฐ€ b์˜ ๋ฐฐ์ˆ˜

4. ๋‘˜ ๋‹ค ์•„๋‹ˆ๋ผ๋ฉด ์•ฝ์ˆ˜, ๋ฐฐ์ˆ˜ ๊ด€๊ณ„๊ฐ€ ์•„๋‹˜

 

5. 2~4๋ฒˆ์„ ํ†ตํ•ด ๋ฌด์Šจ๊ด€๊ณ„์ธ์ง€ ์•Œ์•„๋‚ด๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.

 

6. ๋งŒ์•ฝ a+b==0์ด๋ผ๋ฉด 0 0์„ ์ž…๋ ฅํ–ˆ๋‹ค๋Š” ๋œป์œผ๋กœ ์ข…๋ฃŒํ•œ๋‹ค.


โœ… ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

int main(void) {
	int a, b;

	while (1) {
		scanf("%d%d", &a, &b);
		if (a + b == 0)
			break;
		if (a%b == 0)
			printf("multiple\n");
		else if (b%a == 0)
			printf("factor\n");
		else
			printf("neither\n");
	}
	return 0;
}

https://github.com/2hyunjinn/Baekjoon/blob/47edcc3ebe7db69f35f3e67072d326f9d2447b2d/Baekjoon_5086.cpp

 

GitHub - 2hyunjinn/Baekjoon

Contribute to 2hyunjinn/Baekjoon development by creating an account on GitHub.

github.com