結果

問題 No.193 筒の数式
ユーザー itezpaceitezpace
提出日時 2016-07-08 10:19:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,282 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 71,464 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 01:05:01
合計ジャッジ時間 1,692 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int calc(std::string)’:
main.cpp:14:13: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   14 |   int a,b,f,x;
      |             ^
main.cpp:38:16: warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |         } else if(c=='/'){
      |                ^~
main.cpp:33:12: warning: ‘a’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |           x=a+b;
      |           ~^~~~

ソースコード

diff #

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

int calc(string s){
  if(s[0]<'0' || s[0]>'9'){
    return 0;
  } else if(s[s.size()-2]<'0' || s[s.size()-2]>'9'){
    return 0;
  }
  int a,b,f,x;
  f=0;
  char c,c2;
  string o,p;
  for(int i=0; i<s.size(); ++i){
    if(s[i]>='0' && s[i]<='9'){
      if(f==0){
        o+=s[i];
      } else {
        p+=s[i];
      }
    } else if(s[i]<'0' || s[i]>'9'){
      if(s[i]=='='){
        if(p.size()>1){
          b=stoi(p);
        } else {
          b=p[0]-'0';
        }
        if(c=='+'){
          x=a+b;
        } else if(c=='-'){
          x=a-b;
        } else if(c=='*'){
          x=a*b;
        } else if(c=='/'){
          x=a/b;
        }
      } else {
        if(f==0){
          if(o.size()>1){
            a=stoi(o);
          } else {
            a=o[0]-'0';
          }
          o.clear();
          c=s[i];
          f=1;
        } else {
          if(p.size()>1){
            b=stoi(p);
          } else {
            b=p[0]-'0';
          }
          p.clear();
          if(c=='+'){
            x=a+b;
          } else if(c=='-'){
            x=a-b;
          } else if(c=='*'){
            x=a*b;
          } else if(c=='/'){
            x=a/b;
          }
          a=x;
          c=s[i];
        }
      }
    }
  }
  return x;
}

int main(){
  string s;
  cin>>s;
  int ss,se;
  ss=-1;
  vector<pair<int,int>> vp;
  string s2;
  s2=s;
  s2+='=';
  for(int i=0; i<s2.size(); ++i){
    if(ss==-1 && s[i]>='0' && s[i]<='9'){
      ss=i;
    } else if(ss!=-1 && s[i]<'0' || s[i]>'9'){
      se=i-1;
      if(se-ss>0){
        pair<int,int> p;
        p=make_pair(ss,se);
        vp.push_back(p);
      }
      ss=-1;
    }
  }

  int x,y,z;
  string o;
  o=s;
  o+='=';
  x=calc(o);
  z=x;
  for(int i=0; i<vp.size(); ++i){
    pair<int,int> pa;
    pa=vp[i];
    int l;
    l=pa.second-pa.first;
    for(int j=1; j<=l; ++j){
      string o,o2;
      o=s;
      o2=s;
      rotate(o.begin(),o.begin()+j,o.end());
      o+='=';
      y=calc(o);
      if(y>z){
        z=y;
      }
      rotate(o2.rbegin(),o2.rbegin()+j,o2.rend());
      o2+='=';
      y=calc(o2);
      if(y>z){
        z=y;
      }
    }
  }
  cout<<z<<endl;
  return 0;
}
0