当前位置: 首页 > ds >正文

洛谷P12610 ——[CCC 2025 Junior] Donut Shop

题目背景

Score: 15.

题目描述

The owner of a donut shop spends the day baking and selling donuts.

Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes.

输入格式

The first line of input contains a non-negative integer, D, representing the number of donuts available when the shop first opens.

The second line contains a positive integer, E, representing the number of events that happen over the course of the day. The next E pairs of input lines describe these events. The first line in the pair contains either the + (plus) symbol, indicating that donuts have been baked, or the − (minus) symbol, indicating that donuts have been sold. The second line in the pair contains a positive integer, Q, representing the quantity of donuts associated with the event.

For each sale of donuts, the value of Q will be less than or equal to the number of donuts available at that time.

输出格式

Output the non-negative integer, R, which is the number of donuts remaining when the shop closes.

输入输出样例

输入 #1

10
3
+
24
-
6
-
12

输出 #1

16

说明/提示

Explanation of Output for Sample Input

The shop opened with 10 donuts and there were 3 events during the day. The owner first baked 24 donuts. Then the owner sold 6 donuts, followed by another 12. The number of donuts remaining is 10+24−6−12=16.

这道题是不是有些看不懂?

我来翻译一下

题目背景

分数:15 分。

题目描述 一家甜甜圈店的店主整日都在烘焙和售卖甜甜圈。

根据一天中发生的各种事件,你的任务是确定店铺打烊时剩余的甜甜圈数量。

输入格式 输入的第一行包含一个非负整数D,表示店铺刚开门时可供售卖的甜甜圈数量。

第二行包含一个正整数E,表示一天中发生的事件数量。接下来的E组输入行描述了这些事件。每组的第一行包含 “\(+\)”(加号)符号,表示又烘焙了一些甜甜圈;或者包含 “\(-\)”(减号)符号,表示卖出了一些甜甜圈。每组的第二行包含一个正整数Q,表示与该事件相关的甜甜圈数量。

对于每一次甜甜圈的售卖,Q的值将小于或等于当时可供售卖的甜甜圈数量。

输出格式 输出一个非负整数R,即店铺打烊时剩余的甜甜圈数量。

输入输出样例

输入 #1

10
3
+
24
-
6
-
12

输出 #1

16

说明/提示

店铺一开始有 10 个甜甜圈,一天内发生了 3 件事。店主首先烘焙了 24 个甜甜圈。然后店主卖出了 6 个甜甜圈,接着又卖出了 12 个。剩余的甜甜圈数量为 10 + 24 - 6 - 12 = 16 个。

Problem

你有 D 个甜甜圈,

共有 E 个甜甜圈的变化事件,

每个事件有两种可能的情况:

增加或者减少 Q 个甜甜圈。

求剩余的甜甜圈数量。

Solution

对于每个事件,

对甜甜圈的初始数 D 加或减每个对应的 Q 即可。

那么最终 D 的值即为所求结果。

用循环结构简单模拟就可以,每次输入判断是加还是减法,直接对 D 进行运算即可。

只要判断每次输入的符号是 + 还是 - 即可。

如果是 +

那么总甜甜圈数加上输入的个数。

否则就减去输入的个数。

Code

#include<bits/stdc++.h>
using namespace std;
int main(){int d,e;cin>>d>>e;while(e--){char c;cin>>c;int q;cin>>q;if(c=='+') d+=q;else d-=q;}cout<<d;return 0;
}

听说给点赞,关注,收藏的人会发财哦(づ ̄3 ̄)づ╭❤~

http://www.xdnf.cn/news/10860.html

相关文章:

  • 转战海外 Web3 远程工作指南
  • (10)Fiddler抓包-Fiddler如何设置捕获Firefox浏览器的Https会话
  • 双周报Vol.73:移除使用方法实现 trait 、新增了 “错误多态” 功能、.语法支持使用 _ 的匿名函数...
  • 16QAM在瑞利信道下的性能仿真:从理论到实践的完整解析(附完整代码)
  • 【HarmonyOS 5】鸿蒙Taro跨端框架
  • 【TMS570LC4357】之相关驱动开发学习记录1
  • 总结:线程安全问题的原因和解决方案
  • 初识vue3(vue简介,环境配置,setup语法糖)
  • LlamaIndex的IngestionPipeline添加本地存储(本地文档存储)
  • Unity 环境搭建
  • 【springcloud】快速搭建一套分布式服务springcloudalibaba(四)
  • Python中join()方法完全指南:参数要求与常见用法解析
  • 【深度学习新浪潮】以Dify为例的大模型平台的对比分析
  • 38、响应处理-【源码分析】-HTTPMessageConverter原理
  • C++.双指针算法(1.1目录修正)
  • CA-Net复现
  • 多租户系统的实现方式
  • 第四十天打卡
  • 统计字符数
  • 「Java教案」算术运算符与表达式
  • #16 学习日志软件测试
  • 论文写作核心要点
  • 《高等数学》(同济大学·第7版)第一章第四节《无穷小与无穷大》的超级详细
  • 如何提升大模型召回率和实战案例
  • 页岩油试油试采
  • 深入理解C++面向对象编程:类与对象全面指南
  • 安装DockerDocker-Compose
  • [SC]SystemC中常用的宏和小工具
  • DeepSeek模型多模态扩展与跨模态理解实战
  • 线程池详细解析(一)