CF-B. Catch Overflow!

題目傳送門
簡單題:慘痛教訓。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

typedef long long LL;

#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i = a;i <= b;++i)
#define rep(i,a,b) for(int i = a;i >= b;--i)
const int maxn = 1e5;
int n = 0,m = 0;
char s[10];
LL pre[maxn+10];
int main(){
	while(~scanf("%d",&n)){
		LL ans = 0;
		int cnt = 0;
		pre[0] = 1ll;

		per(i,1,n){
			scanf("%s",s);
			if(s[0] == 'f'){
				scanf("%d",&m);
				if(pre[cnt] * m < (1ll<<32) && pre[cnt] != -1){
					pre[cnt+1] = pre[cnt] * m;
					++cnt;	
				}else{
					pre[++cnt] = -1;
				}
			}else if(s[0] == 'a'){
				if(pre[cnt] == -1){
					return puts("OVERFLOW!!!"),0;
				}
				ans += pre[cnt];
				if(ans > (1ll<<32)-1){
					return puts("OVERFLOW!!!"),0;
				}
			}else{
				--cnt;
			}
		}
		printf("%I64d\n",ans);
	}
	
	return 0;
}

自己寫的:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

typedef long long LL;

#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i = a;i <= b;++i)
#define rep(i,a,b) for(int i = a;i >= b;--i)
const int maxn = 1e5;
int n = 0;
char s[10];
LL mul = 1;
int m[maxn+10];

int main(){
	while(~scanf("%d",&n)){
		LL ans = 0;
		bool fg = false;
		int cnt = 0,times = 0;
		mul = 1;
		per(i,1,n){
			scanf("%s",s);
			if(s[0] == 'f'){
				scanf("%d",&m[++cnt]);
				if(mul * m[cnt] > (1ll<<32)-1 || fg){//如果之前也是超出,那麼也記錄,所以要|| fg
					fg = true;
					++times;
					continue;
				}
				mul *= m[cnt];
			}else if(s[0] == 'a'){
				ans += mul;
				if(ans > (1ll<<32)-1 || fg == true){
					return puts("OVERFLOW!!!"),0;
				}
			}else{
				if(times > 0){
					--times;
					--cnt;
					if(times == 0){
						fg = false;
					}
					continue;
				}else{
					fg = false;
				}
				mul /= m[cnt--];
			}
		}
		printf("%I64d\n",ans);
	}
	
	return 0;
}
/*

13
for 100
for 100
for 100
for 100
for 100
for 2
end
add
end
end
end
end
end
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章