結果

問題 No.193 筒の数式
ユーザー 158b158b
提出日時 2015-05-26 12:33:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,401 bytes
コンパイル時間 502 ms
コンパイル使用メモリ 66,384 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 13:28:37
合計ジャッジ時間 1,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <limits.h>
#include <vector>
#include <numeric>
using namespace std;

long func(string a){
	long total = 0;
	long num = 0;
	bool flg = false;	//左の項が存在しているか。
	bool op = true;		//true足し算/false引き算
	
	for(int i=0; i<a.length(); i++){
		//数値が見つかった場合
		if(a[i] !=  '+' && a[i] != '-'){
			flg = true;
			num *= 10;
			num += a[i] - '0';
			
		//記号が見つかった場合
		}else{
			if(!flg){
				return -LONG_MAX;
			}else{
				if(op){
					total += num;
				}else{
					total -= num;
				}
				num = 0;
				flg = false;
				if(a[i] == '+'){
					op = true;
				}else{
					op = false;
				}
			}
		}
	}
	
	//最後の項が未処理
	if(!flg){
		return -LONG_MAX;
	}else{
		if(op){
			total += num;
		}else{
			total -= num;
		}
	}
	
	return total;
}


int main(){
	string data;
	long kekka;
	long ans = -LONG_MAX;
	char save;
	
	cin >> data;
	
	for(int i=0; i<data.length(); i++){
		//cout << "■" << data << endl;
		kekka = func(data);
		//cout << "result:" << kekka << endl;
		//ans更新
		if(kekka > ans){
			ans = kekka;
		}
		
		//文字列をずらす処理(左循環シフト)
		save = data[0];
		for(int ii=0; ii<data.length()-1; ii++){
			data[ii] = data[ii+1];
		}
		data[data.length() - 1] = save;
		
	}
	
	cout << ans << endl;
	return 0;
}
0