Algorithm/Baekjoon

[๋ฐฑ์ค€ 10815] C++ - ๋™์ ์œผ๋กœ ๋ฐฐ์—ด ํ• ๋‹นํ•˜๊ธฐ, ์ด์ง„ ํƒ์ƒ‰, ์ž…์ถœ๋ ฅ ์†๋„ ํ–ฅ์ƒ

say! 2024. 7. 11. 17:09
728x90

 

๐ŸŸกํ‹€๋ฆฐ ์ฝ”๋“œ

segment error๋‚˜์˜ด, ๋ฐฐ์—ด์ด ๋„ˆ๋ฌด ์ปค์„œ ์Šคํƒ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋ผ๊ณ  ํ•œ๋‹ค.

>> ๋™์ ์œผ๋กœ ๋ฐฐ์—ด ํ• ๋‹นํ•ด์„œ ํ•ด๊ฒฐํ•˜๊ธฐ

#//๋ฐฑ์ค€ 10815 - ์ˆซ์ž ์นด๋“œ
#include <iostream>
using namespace std;

int main(){
  int n, m;
  int have[500000], cards[500000];

  // ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ ์ž…๋ ฅ
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> have[i];
  }

  // ๋น„๊ตํ•  ์นด๋“œ ์ž…๋ ฅ
  cin >> m;
  int i;
  for(i = 0; i < m; i++){
    cin >> cards[i];

    for(int j = 0; j < n; j++){
      if(have[j] == cards[i]){
        cards[i] = 1;
        break;
      }
      if(j == n-1) cards[i] = 0;
    }
  }

  // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  for(int i = 0; i < m; i++){
    cout << cards[i] << " ";
  }

  return 0;
}

 

๐ŸŸกํ‹€๋ฆฐ ์ฝ”๋“œ

์ด์ค‘ for๋ฌธ์œผ๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ

>>์ด์ง„ ํƒ์ƒ‰ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐ

 

c++ ๊ธฐ์ดˆ(๋™์  ๋ฐฐ์—ด ํ• ๋‹น)

์ด๊ธ€์€ "์ „๋ฌธ๊ฐ€๋ฅผ ์œ„ํ•œ c++(๊ฐœ์ •4ํŒ)"์„ ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ์ง์ ‘ ์‹ค์Šตํ•ด๋ณด๋ฉฐ ์ •๋ฆฌํ•œ Review์šฉ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋™์  ๋ฐฐ์—ด ํ• ๋‹น ์šฉ๋Ÿ‰์ด ํฐ ๋ฐฐ์—ด๊ฐ™์€ ๊ฒฝ์šฐ ๋ณดํ†ต ํž™์— ๋™์  ํ• ๋‹น์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ new ์—ฐ์‚ฐ์ž์™€ []๋ฅผ

todamfather.tistory.com

#//๋ฐฑ์ค€ 10815 - ์ˆซ์ž ์นด๋“œ
#include <iostream>
using namespace std;

int main(){
  int n, m;
  
  // ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ ์ž…๋ ฅ
  cin >> n;
  int* have = new int[n];
  for(int i = 0; i < n; i++){
    cin >> have[i];
  }

  // ๋น„๊ตํ•  ์นด๋“œ ์ž…๋ ฅ
  cin >> m;
  int* cards = new int[m];
  int i;
  for(i = 0; i < m; i++){
    cin >> cards[i];

    for(int j = 0; j < n; j++){
      if(have[j] == cards[i]){
        cards[i] = 1;
        break;
      }
      if(j == n-1) cards[i] = 0;
    }
  }

  // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  for(int i = 0; i < m; i++){
    cout << cards[i] << " ";
  }

  return 0;
}

 

 

๐Ÿ”ต์ˆ˜์ • ์ฝ”๋“œ >> ์‹œ๊ฐ„ ์ดˆ๊ณผ

์ด์ง„ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ์ •๋ ฌํ•˜๊ธฐ

#include <algorithm> ํ•„์š”

 

[C++] STL ์ •๋ ฌ(Sort) ํ•จ์ˆ˜

C++์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ ฌ ํ•จ์ˆ˜๋“ค์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

velog.io

๋™์  ํ• ๋‹น ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ์ •๋ ฌํ•˜๋Š” ๋ฒ•

cin >> n;
  int* have = new int[n];
  for(int i = 0; i < n; i++){
    cin >> have[i];
  }

  sort(have, have + n);
 

[C++]๋™์ ํ• ๋‹นํ•œ ํด๋ž˜์Šค ๋ฐฐ์—ด sort๋กœ ์ •๋ ฌํ•˜๊ธฐ

STL vector๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ๋‚˜์„œ๋ถ€ํ„ฐ ์ •์ ๋ฐฐ์—ด๊ณผ ๋™์ ํ• ๋‹น์€ ๋‹ค์†Œ ์ฐฌ๋ฐฅ์‹ ์„ธ๊ฐ€ ๋˜์—ˆ์ง€๋งŒ.. ๊ทธ๋ ‡๋‹ค๊ณ  ํ‰์ƒ ์“ธ ...

blog.naver.com

#//๋ฐฑ์ค€ 10815 - ์ˆซ์ž ์นด๋“œ
#include <iostream>
using namespace std;
#include <algorithm>

int main(){
  int n, m, num;
  
  // ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ ์ž…๋ ฅ
  cin >> n;
  int* have = new int[n];
  for(int i = 0; i < n; i++){
    cin >> have[i];
  }

  sort(have, have + n); // ์ •๋ ฌ

  // ์ด์ง„ ํƒ์ƒ‰
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> num;

    cout << binary_search(have, have+n, num) << " ";
  }

  return 0;
}

์œ„์ฒ˜๋Ÿผ binary_search๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜์˜ด

 

>>์ž…์ถœ๋ ฅ ๋ถ€๋ถ„ ์ˆ˜์ •

#//๋ฐฑ์ค€ 10815 - ์ˆซ์ž ์นด๋“œ
#include <iostream>
using namespace std;
#include <algorithm>

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

  int n, m, num;
  
  // ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ ์ž…๋ ฅ
  cin >> n;
  int* have = new int[n];
  for(int i = 0; i < n; i++){
    cin >> have[i];
  }

  sort(have, have + n); // ์ •๋ ฌ

  // ์ด์ง„ ํƒ์ƒ‰
  cin >> m;
  for(int i = 0; i < m; i++){
    cin >> num;

    cout << binary_search(have, have+n, num) << " ";
  }
  
  delete[] have;
  return 0;
}

 

ios_base::sync_with_stdio(false); ๋Š” c์™€ c++ ํ‘œ์ค€ stream์˜ ๋™๊ธฐํ™”๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…์ถœ๋ ฅ ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ๋Š” cin, cout๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

 

 

ios::sync_with_stdio(false), cin.tie(0) ์“ฐ๋Š” ์ด์œ , ๋ฐฑ์ค€ ์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ

1. ios::sync_with_stdio(false), cin.tie(0) ์€ ๋ฌด์—‡์ธ๊ฐ€? ๋ณดํ†ต ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ™์€ ์˜จ๋ผ์ธ ์ €์ง€ ์‚ฌ์ดํŠธ์—์„œ C++์„ ์ด์šฉํ•˜์—ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ๋‘ ์ค„์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ

dingcoding.tistory.com