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
*/
<template> <div class="box bgc3 sku-sale-rank"> <!-- TODO: 分辨大怎么展示问UI --> <div class="header"> <div class="title"> 商品热榜<span class="sub-title">{{ start }} ~ {{ end }}</span> </div> </div> <div class="body"> <el-row v-for="(item, index) in skuSaleRank" :key="index"> <el-col :span="5"> <div :class="'top top' + (index + 1)"> {{ index + 1 }} </div> </el-col> <el-col :span="13"> <div class="sku-name" :title="item.skuName"> {{ item.skuName }} </div> </el-col> <el-col :span="6"> <div class="count">{{ item.count }}单</div> </el-col> </el-row> </div> </div> </template> <script setup> import dayjs from "dayjs"; // 定义变量 const skuSaleRank = ref([ { skuId: "0", skuName: "可口可乐", count: 9, amount: 0, }, { skuId: "0", skuName: "营养快线", count: 8, amount: 0, }, { skuId: "0", skuName: "阿萨姆奶茶", count: 6, amount: 0, }, { skuId: "0", skuName: "星巴克摩卡味", count: 2, amount: 0, }, { skuId: "0", skuName: "美汁源果粒橙", count: 1, amount: 0, }, { skuId: "0", skuName: "怡宝", count: 1, amount: 0, }, ]); const start = dayjs().startOf("month").format("YYYY.MM.DD"); const end = dayjs().endOf("day").format("YYYY.MM.DD"); </script> <style lang="scss" scoped> @import "@/assets/styles/variables.module.scss"; .sku-sale-rank { display: flex; flex-direction: column; height: calc((100vh - 120px) * 0.6); min-height: 538px; background: #ffffff; border-radius: 20px; .body { flex: 1; display: flex; flex-direction: column; justify-content: space-between; margin-top: 20px; .top { display: inline-block; width: 16px; height: 20px; margin-left: 10px; background: url("@/assets/user-task-stats/top.png"); text-align: center; font-size: 12px; font-weight: normal; color: #e9b499; line-height: 14px; } .top1 { width: 21px; height: 20px; background: url("@/assets/user-task-stats/top1.png"); color: #8e5900; } .top2 { width: 21px; height: 20px; background: url("@/assets/user-task-stats/top2.png"); color: #494949; } .top3 { width: 21px; height: 20px; background: url("@/assets/user-task-stats/top3.png"); color: #cf6d3d; } .sku-name { height: 20px; font-size: 14px; font-weight: 500; color: $--color-text-primary; line-height: 20px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } .count { height: 20px; font-size: 14px; font-family: PingFangSC-Regular, PingFang SC; font-weight: 400; color: #737589; line-height: 20px; text-align: right; } } } </style> 基于后端接口export function getTopSelling() { return request({ url: "/manage/order/top-selling", method: "get", }); }改造前端代码,使其调用接口动态获取数据而不是写死
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值