結果

問題 No.222 引き算と足し算
ユーザー nanasili
提出日時 2015-07-31 20:54:50
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,162 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 82,544 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 22:00:42
合計ジャッジ時間 1,618 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:31:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |   scanf("%s", str);
      |   ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

#define PLUS 1
#define MINUS 2

int main() {
  char str[15];
  scanf("%s", str);
  int op = 0;
  int val1, val2;
  int sp = !isdigit(str[0]);
  val1 = val2 = 0;
  for (int i = sp; str[i] != '\0'; i++) {
    if (isdigit(str[i-1]) &&
        (str[i] == '+' || str[i] == '-')) {
      if (str[i] == '+') {
        op = MINUS;
      }else {
        op = PLUS;
      }
      sp = !isdigit(str[i+1]);
      for (int j = i+1+sp; str[j] != '\0'; j++) {
        val2 = val2*10+(str[j]-'0');
      }
      if (op == MINUS) {
        val2 *= -1;
      }
      if (str[i+1] == '-') {
        val2 *= -1;
      }
      if (str[0] == '-') {
        val1 *= -1;
      }
      printf("%d\n", val1+val2);
      return 0;
    }
    val1 = val1*10+(str[i]-'0');
  }

}
0