Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3-kokeunho #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

3-kokeunho #14

wants to merge 1 commit into from

Conversation

kokeunho
Copy link
Collaborator

@kokeunho kokeunho commented Oct 7, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

[BOJ] ์•ฝ์ˆ˜์˜ ํ•ฉ https://www.acmicpc.net/problem/9506

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

-1์ด ์ž…๋ ฅ๋˜๊ธฐ ์ „๊นŒ์ง€ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ  ์™„์ „์ˆ˜์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ •์ˆ˜ n์ด ์ž…๋ ฅ๋˜์—ˆ๋‹ค๋ฉด i์—์„œ n๊นŒ์ง€ ๋Œ๋ฉด์„œ n % i๊ฐ€ 0์ธ ์•ฝ์ˆ˜ i๋ฅผ ์ฐพ๊ณ  ๋ฆฌ์ŠคํŠธ factors์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ์•ฝ์ˆ˜๋“ค์˜ ํ•ฉ(sum)์ด n๊ณผ ๊ฐ™๋‹ค๋ฉด
์š”๊ตฌ ์–‘์‹๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ณ 
๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด is not perfect๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํŒŒ์ด์ฌ ๊ธฐ์ดˆ๋ฅผ ์ข€ ๋‹ค์ง€๋ ค๊ณ  ์‰ฌ์šด ๋ฌธ์ œ๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

์ถœ๋ ฅ ์–‘์‹์„ ๋งž์ถ”๋ ค๊ณ  ์—ฌ๋Ÿฌ ์ค„ ์ผ๋Š”๋ฐ

print(f "{n} =" + " + " .join(map(str, factors)))

์š”๋ ‡๊ฒŒ ํ•œ ์ค„์ด๋ฉด ๋™์ผํ•œ ์–‘์‹์œผ๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค...

" + ".join()

์€ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ " + "๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค

@kokeunho kokeunho self-assigned this Oct 7, 2024
@kokeunho kokeunho changed the title 2024-10-08 ์•ฝ์ˆ˜์˜ ํ•ฉ 3-kokeunho Oct 7, 2024
Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” cpp ํ’€์ด์ž…๋‹ˆ๋‹ค. ๊ณ ์ƒํ•˜์…จ์”๋‹ˆ๋‹น

code
#include <iostream>
#include <vector>	
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int num;
	cin >> num;
	while (num != -1) {
		vector<int> divisor;
		int sum = num;
		for (int i = 1; i < num; i++) {
			if (num % i == 0) {
				divisor.push_back(i);
				sum -= i;
			}
		}
		if (!sum) {
			cout << num << " = " << divisor[0];
			for (int i = 1; i < divisor.size(); i++) cout << " + " << divisor[i];
			cout << '\n';
		}
		else cout << num << " is NOT perfect.\n";
		cin >> num;
	}
	return 0;
}

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋” ์žฌ๋ฏธ์žˆ๋Š” ๋ฌธ์ œ๋„ ๊ธฐ๋Œ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

c++ ์ฝ”๋“œ๋ณด๊ธฐ
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    while (1) {
        cin >> n;
        if (n == -1) break;

        vector<int> v;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            if (n % i == 0) {
                sum += i;
                v.push_back(i);
            }
        }

        cout << n;
        if (sum != n)  cout << " is NOT perfect.\n";
        else {
            cout << " = " << v[0];
            for (int i = 1; i < v.size(); i++) cout << " + " << v[i];
            cout << '\n';
        }
    }
    return 0;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ตฌํ˜„ ๋ฌธ์ œ๊ตฐ์š”.
์–ธ์–ด๋ฅผ ์ตํžˆ๋ฉด์„œ ๊ฐ€๊ธฐ์— ์ข‹์€ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐ ๋ฉ๋‹ˆ๋‹ค.
์ œ c++ ์ฝ”๋“œ๋ž‘ ํ’€์ด ๋ฐฉ์‹์€ ์™„๋ฒฝํžˆ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants