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

17-oesnuj #52

Merged
merged 3 commits into from
Oct 7, 2024
Merged

17-oesnuj #52

merged 3 commits into from
Oct 7, 2024

Conversation

oesnuj
Copy link
Member

@oesnuj oesnuj commented Sep 21, 2024

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

๋ฐฑ์ค€ | ํฐ ์ˆ˜ A+B(2)

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

30๋ถ„

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

์ตœ๋Œ€ 10์˜ 10000 ์ œ๊ณฑ์„ ๋”ํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ์—, int๋‚˜ long long int์™€ ๊ฐ™์€ ์ •์ˆ˜ํ˜• ์ž๋ฃŒํ˜•์œผ๋กœ ๋”ํ•˜๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด(String)๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์€ ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ˆซ์ž์˜ ํฌ๊ธฐ์™€ ์ƒ๊ด€์—†์ด ์ •ํ™•ํ•œ ๋ง์…ˆ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์ดˆ๋“ฑํ•™์ƒ ๋•Œ ๋ฐฐ์› ๋˜ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ, ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋”ํ•˜๊ณ , ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ง์…ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ๋‘ ์ˆ˜์˜ ๋ฌธ์ž์—ด ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. (a, b)
  2. ์ž๋ฆฌ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด ๋์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ฐ ์ž๋ฆฌ์˜ ์ˆซ์ž๋ฅผ ๋”ํ•˜๊ณ , carry(์˜ฌ๋ฆผ์ˆ˜)๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฅผ ๋‹ค์Œ ์ž๋ฆฌ ๊ณ„์‚ฐ์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.
  4. ๋‘ ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์ž๋ฆฌ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ , ๋‚จ์•„์žˆ๋Š” carry๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.
  5. ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์„ ํ†ตํ•ด ๋ง์…ˆ์„ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋ฉฐ ๊ฒฐ๊ตญ ๋‘ ์ˆ˜์˜ ํ•ฉ์„ ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“‹ ์ „์ฒด ์ฝ”๋“œ

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// ๋‘ ํฐ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ํ•จ์ˆ˜
string Add(string str1, string str2)
{
    string result = ""; // ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฌธ์ž์—ด
    int i = str1.size() - 1; // ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๋ถ€ํ„ฐ ์‹œ์ž‘
    int j = str2.size() - 1; // ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๋ถ€ํ„ฐ ์‹œ์ž‘
    int carry = 0; // ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜

    // ๋‘ ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต (๋‚จ์•„ ์žˆ๋Š” carry๋„ ์ฒ˜๋ฆฌ)
    while (i >= 0 || j >= 0 || carry > 0)
    {
        int num1 = 0, num2 = 0;

        // str1์˜ ๋‚จ์€ ์ž๋ฆฟ์ˆ˜ ์ฒ˜๋ฆฌ
        if (i >= 0)
            num1 = str1[i--] - '0'; // ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜

        // str2์˜ ๋‚จ์€ ์ž๋ฆฟ์ˆ˜ ์ฒ˜๋ฆฌ
        if (j >= 0)
            num2 = str2[j--] - '0'; // ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜

        // ๋‘ ์ž๋ฆฌ ์ˆซ์ž์™€ carry๋ฅผ ๋”ํ•จ
        int sum = num1 + num2 + carry;
        carry = sum / 10; // ์˜ฌ๋ฆผ์ˆ˜ ๊ณ„์‚ฐ
        result = to_string(sum % 10) + result; // ํ˜„์žฌ ์ž๋ฆฟ์ˆ˜ ์ €์žฅ (๋’ค์—์„œ ์•ž์œผ๋กœ ๋”ํ•จ)
    }
    return result; // ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
}


int main()
{
    string a, b;
    cin >> a >> b;
    cout << Add(a, b);
    return 0;
}

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

๋จธ๋ฆฌ ์†์œผ๋กœ๋Š” ์‰ฝ์ง€๋งŒ ๋ง‰์ƒ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ๋จธ๋ฆฌ๋ฅผ ๊ฝค ์จ์•ผํ–ˆ๋„ค์š”.

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๊ฐ€ ์งง๊ธธ๋ž˜ ์‰ฝ๊ฒŒ ๋‹ค๊ฐ€๊ฐ”๋‹ค๊ฐ€ ์ €๋„ ํ ์นซํ–ˆ๋„ค์š”... ์ค€์„œ๋‹˜์€ ๋ฌธ์ž์—ด๋กœ ํ•ด๊ฒฐํ•˜์…จ๋Š”๋ฐ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋”๋ผ๊ณ ์š”!

๋ฐฐ์—ด(C99)

  • ๋ฌธ์ž์—ด์„ ์บ๋ฆญํ„ฐ ๋ฐฐ์—ด๋กœ ์ฒ˜๋ฆฌ
  • strlen() ์„ ์ด์šฉํ•ด์„œ ๊ธธ์ด ๊ณ„์‚ฐ
  • ๊ฐ ๋ฌธ์ž๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ธ๋ฑ์‹ฑํ•ด์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
  • ๋ง์…ˆ ๊ฒฐ๊ณผ๋ฅผ ์ •์ˆ˜ ๋ฐฐ์—ด์— ์ €์žฅ
  • ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์„ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ

๊ทธ์น˜๋งŒ ์ค€์„œ๋‹˜์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ํ›จ์”ฌ ์ข‹์•„ ๋ณด์ž…๋‹ˆ๋‹ค... โ—  โ—  pr ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

Copy link
Collaborator

@suhyun113 suhyun113 left a comment

Choose a reason for hiding this comment

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

๋งค์šฐ ํฐ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ๋ฌธ์ œ๊ตฐ์š”, ๋‹น์—ฐํžˆ ์ˆซ์ž๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋ผ intํ˜•์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋‹ˆ, ๋†€๋ž์Šต๋‹ˆ๋‹ค. ๋ถ„๋ช… ๋ฐฐ์šด์  ์žˆ๋Š” ๋‚ด์šฉ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ์‹ค์ œ๋กœ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ฒ˜์Œ ๋ณด์•„์„œ ์‹ ๊ธฐํ–ˆ์–ด์š”. ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ค€์„œ๋‹˜์ฒ˜๋Ÿผ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , ์ž๋ฆฌ ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋”ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์š”.

์ด๋ฒˆ PR์—์„œ, ํฐ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ์‹๋„ ์ ์ ˆํ–ˆ๊ณ , ์ดˆ๋“ฑํ•™๊ต๋•Œ ๋ฐฐ์šด ์ž๋ฆฌ์ˆ˜ ๋ง์…ˆ ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ฆฌ๋ฉฐ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๊ฒฐํ•œ ์ ์ด ์ข‹์•„๋ณด์˜€์–ด์š”. ํŠนํžˆ ๋ฌธ์ž์—ด ๋์—์„œ๋ถ™ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ , ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ์ž˜ ๊ตฌํ˜„๋˜์–ด์žˆ๋„ค์š”!
์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ์‹๋„ ์ ์ ˆํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๊ตฌ ์ € ๋˜ํ•œ ์ดˆ๋“ฑํ•™๊ต๋•Œ ๋ฐฐ์šด ์ž๋ฆฌ์ˆ˜ ๋ง์…ˆ ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ฆฌ๋ฉฐ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๊ฒฐํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹คใ… ํŠนํžˆ ๋ฌธ์ž์—ด ๋์—์„œ๋ถ™ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ , ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ์ž˜ ๊ตฌํ˜„๋˜์–ด์žˆ๋„ค์š”!

์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”! ๋ฆฌ๋ทฐ๊ฐ€ ๋„ˆ๋ฌด ๋Šฆ์–ด์ ธ์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค.๐Ÿ˜ญ๋•๋ถ„์— ์ €๋„ ํฐ ์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค์‹œ ๋– ์˜ฌ๋ฆฌ๊ฒŒ ๋˜์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”.

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