ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] ๊ฐ€์žฅ ํฐ ์ˆ˜
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 2019. 9. 21. 20:45
    728x90

    ๋ฌธ์ œ ์„ค๋ช…

    0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด ์ฃผ์„ธ์š”.

    ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์ •์ˆ˜๊ฐ€ [6, 10, 2]๋ผ๋ฉด [6102, 6210, 1062, 1026, 2610, 2106]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋Š” 6210์ž…๋‹ˆ๋‹ค.

    0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ˆœ์„œ๋ฅผ ์žฌ๋ฐฐ์น˜ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

    ์ œํ•œ ์‚ฌํ•ญ

    • numbers์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • numbers์˜ ์›์†Œ๋Š” 0 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ์ •๋‹ต์ด ๋„ˆ๋ฌด ํด ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•ฉ๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ

    numbers return
    [6, 10, 2] 6210
    [3, 30, 34, 5, 9] 9534330

     

     

    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    bool cmp(string& a, string& b)
    {
    	return a+b > b+a ? true : false; 
    }
    string solution(vector<int> numbers) {
    	string answer = "";
    	vector<string> number_array;
    
    	for (int i = 0; i < numbers.size(); ++i)
    		number_array.push_back(to_string(numbers.at(i)));
    	/*for (int i : numbers)
    		number_array.push_back(to_string(numbers.at(i)));*/
    
    	sort(number_array.begin(), number_array.end(), cmp);
    
    	for (int i = 0; i < number_array.size(); ++i)
    		answer += number_array.at(i);
    	/*for (auto iter = number_array.begin; iter != number_array.end; ++iter)
    		answer += *iter;*/
    
    	if (answer.front() == '0')
    		answer = "0";
    
    	return answer;
    }
    int main()
    {
    	vector<int> numbers = {3, 30, 34, 5, 9};
    	string ans;
    	
    	ans = solution(numbers);
    	cout << ans << endl;
    }
    728x90

    ๋Œ“๊ธ€

Designed by Tistory.