結果

問題 No.39 桁の数字を入れ替え
ユーザー kichirb3kichirb3
提出日時 2018-03-09 22:10:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,129 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 79,608 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 11:41:23
合計ジャッジ時間 1,479 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.39 桁の数字を入れ替え
// https://yukicoder.me/problems/no/39
//

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

vector<int> find_nums(vector<int>& numbers, int target);


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    string s;
    cin >> s;
    vector<int> N;
    for (char c: s) {
        N.push_back(c - '0');
    }
    vector<int> largest = N;
    sort(largest.rbegin(), largest.rend());

    int t1 = 0;
    int t2 = 0;
    for (auto i = 0; i < N.size(); ++i) {
        if (N[i] != largest[i]) {
            t1 = largest[i];
            t2 = N[i];
            break;
        }
    }

    if (t1 != 0) {
        vector<int> p1 = find_nums(N, t1);
        vector<int> p2 = find_nums(N, t2);
        swap(N[*(p1.end()-1)], N[*(p2.begin())]);
    }

    for (auto i: N)
        cout << i;
    cout << endl;
}


vector<int> find_nums(vector<int>& numbers, int target) {
    vector<int> res;
    for (int i = 0; i < numbers.size(); ++i) {
        if (numbers[i] == target)
            res.push_back(i);
    }
    return res;
}
0