結果

問題 No.222 引き算と足し算
コンテスト
ユーザー ferin
提出日時 2017-03-01 04:30:35
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,344 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,217 ms
コンパイル使用メモリ 181,520 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2026-03-05 06:28:26
合計ジャッジ時間 2,151 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:48:13: warning: ignoring return value of 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::substr(size_type, size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
   48 |     a.substr(1, a.size()-1);
      |     ~~~~~~~~^~~~~~~~~~~~~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:56,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bitset:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/basic_string.h:3441:7: note: declared here
 3441 |       substr(size_type __pos = 0, size_type __n = npos) const
      |       ^~~~~~
main.cpp:58:13: warning: ignoring return value of 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::substr(size_type, size_type) const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
   58 |     b.substr(1, b.size()-1);
      |     ~~~~~~~~^~~~~~~~~~~~~~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/basic_string.h:3441:7: note: declared here
 3441 |       substr(size_type __pos = 0, size_type __n = npos) const
      |       ^~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF (1LL<<25)     //33554432
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

signed main(void)
{
  string s, a, b;
  cin >> s;
  bool plus = false, flag = false;
  REP(i, s.size()) {
    if(s[i] >= '0' && s[i] <= '9') flag = true;
    if(flag && s[i] == '+') {
      plus = true;
      a = s.substr(0, i);
      b = s.substr(i+1, s.size()-i-1);
      break;
    }
    if(flag && s[i] == '-') {
      plus = false;
      a = s.substr(0, i);
      b = s.substr(i+1, s.size()-i-1);
      break;
    }
  }

  int c, d;
  bool e = false, f = false;
  if(a[0] == '-') {
    e = true;
    a = a.substr(1, a.size()-1);
  }
  if(a[0] == '+') {
    a.substr(1, a.size()-1);
  }
  c = stoi(a);
  if(e) c *= -1;

  if(b[0] == '-') {
    f = true;
    b = b.substr(1, b.size()-1);
  }
  if(b[0] == '+') {
    b.substr(1, b.size()-1);
  }
  d = stoi(b);
  if(f) d *= -1;

  //cout << c << " " << d << endl;
  if(plus) {
    cout << c-d << endl;
  } else {
    cout << c+d << endl;
  }

  return 0;
}
0