結果
問題 | No.297 カードの数式 |
ユーザー |
![]() |
提出日時 | 2016-09-21 01:28:01 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 1,740 bytes |
コンパイル時間 | 440 ms |
コンパイル使用メモリ | 61,792 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-26 02:32:13 |
合計ジャッジ時間 | 1,247 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
#include <iostream> #include <cmath> #define pr(s) std::cout << s << std::endl int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n; std::cin >> n; int c[10] = { 0 }; int d[10] = { 0 }; char t; int minus = 0, plus = 0; for (int i = 0; i < n; ++i) { std::cin >> t; if (t == '-') { ++minus; } else if (t == '+') { ++plus; } else { ++c[t - 48]; ++d[t - 48]; } } long long max = 0, min = 0; for (int i = 0; i < n - 2*(minus + plus); ++i) { for (int j = 9; j >= 0; --j) { if (c[j] > 0) { max = max * 10 + j; --c[j]; break; } } } for (int i = 0; i < minus; ++i) { for (int j = 0; j < 10; ++j) { if (c[j] > 0) { max -= j; --c[j]; break; } } } for (int i = 0; i < 10; ++i) { max += c[i] * i; } std::cout << max << " "; if (minus > 0) { for (int i = 0; i < n - 2*(minus + plus); ++i) { for (int j = 9; j >= 0; --j) { if (d[j] > 0) { min = min * 10 + j; --d[j]; break; } } } min *= -1; --minus; for (int i = 0; i < minus; ++i) { for (int j = 9; j >= 0; --j) { if (d[j] > 0) { min -= j; --d[j]; break; } } } for (int i = 0; i < 10; ++i) { min += d[i] * i; } } else { int sepa = (n - plus) / (plus + 1); int sepaer = (n - plus) - sepa*(plus + 1); for (int i = 0; i < sepaer; ++i) { for (int j = 0; j < 10; ++j) { if (d[j] > 0) { min += j * pow(10, sepa); --d[j]; break; } } } for (int i = sepa - 1; i >= 0; --i) { for (int j = 0; j < plus + 1; ++j) { for (int k = 0; k < 10; ++k) { if (d[k] > 0) { min += k * pow(10, i); --d[k]; break; } } } } } std::cout << min << std::endl; }