UESTC 1647 Battery Charging (找规律模拟)

探讨一款手机配备无限容量电池的特性及其使用策略,包括每日充电与使用选择,最终计算用户可在售出前的最大使用天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Recently, qbwj bought a powerful cellphone which performs even better than most computers. He spent lots of time on it. However, this cellphone has a very strange feature which confused qbwj. There is a battery with infinite capacity in the cellphone. On each day, qbwj has three choices: use the cellphone, charge the battery or do nothing. He cannot do using and charging on the same day. Charging on the kth day adds k units of power to the battery. If qbwj chooses to use the cellphone on the kth day, it would consume k units of power. Note that qbwj can choose to use the cellphone if and only if there are enough units of power in the battery.

Despite of the strengths of the cellphone, qbwj couldn't suffer it any more. So he decides to sell it at the end of Tth day. Today is the Sth day and the battery is empty now. He wants to know how many days at maximum he can use the cellphone before selling it out.

Note that qbwj can still choose to use the cellphone on both the Sth day and Tth day.

Input

The first line of the input will be an integer N (N <= 10000) indicating the number of cases.
For each test case, two integers are given on a single line: S T. 1 <= S <= T <= 108.

Output

Print "Case #k: d" in a single line for each test case, in which k represents the case number which starts from 1, and d is the answer.

Sample Input

3
3 6
3 9
1 100000

Sample Output

Case #1: 1
Case #2: 3
Case #3: 49994

Hint

For the first sample, we charge the cellphone on the 3rd, 4th, 5th day and use it on the 6th day.

Source

10th UESTC Programming Contest Final 

分析:简单贪心,显然电池能用则用 
因为 1、以后用要花费更多的电量;2、以后充电比现在充电要得到更多的电
量。 
第 S 天电池没电,充完电之后剩余 S 的电量 
第 S+1 天因为电量不够还需要充电,充完电之后剩下 2*S+1 的电量 
第 S+2 天使用电池,用完之后剩下 S-1 的电量,以后重复这样的步骤,也就
是说充一天电,使用一天。每进行一次这样的步骤,剩余电量减 1。所以 S 的电
量可以进行 S 次这样的步骤,也就使用了 S 天。第 3*S 天,剩余电量为 0 。然
后第 3*S+1 天又是相同的问题。因为每次 3 倍的增长,直接模拟就好了。

我的代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
    int t,cas=1,k,a,b,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d%d",&a,&b);
        while(a<b)
        {
            k=3*a;
            if(k<=b)
            {
                ans+=a;
                a=3*a+1;
            }
            else
            {
                ans+=(b-a)/2;break;
            }
        }
        printf("Case #%d: %d\n",cas++,ans);
    }
}
 				    


资源下载链接为: https://pan.quark.cn/s/9648a1f24758 今天想和大家分享一下 Linux 操作系统中的防火墙相关知识。防火墙在 Linux 系统里扮演着极为关键的角色,它就像是系统安全的“守门人”。通过合理配置防火墙,可以有效控制网络流量的进出,阻止恶意攻击,保护系统内部的重要数据和资源不受侵害。 在 Linux 系统中,常见的防火墙工具有 iptables 等。iptables 是一个功能强大的防火墙工具,它通过定义一系列规则来对网络数据包进行过滤、转发、修改等操作。这些规则可以根据数据包的源地址、目的地址、协议类型、端口号等多种因素来制定,从而实现灵活的网络流量控制。 例如,我们可以使用 iptables 设置规则,只允许特定的 IP 地址访问系统的某些服务端口,如 SSH 端口,这样可以防止未经授权的用户通过暴力破解等方式登录系统。同时,也可以设置规则阻止系统对外部某些恶意网站或 IP 的访问,避免系统受到网络钓鱼等攻击。 除了 iptables,还有其他一些防火墙工具可供选择,如 firewalld。firewalld 是一个动态管理防火墙的工具,它提供了更易于管理的接口和更灵活的配置方式。它可以方便地定义不同的防火墙区域,每个区域可以设置不同的安全策略,从而更好地适应复杂的网络环境。 学习 Linux 防火墙不仅可以提升系统的安全性,还可以帮助我们更好地理解网络通信的原理和机制。希望大家都能重视起 Linux 防火墙的学习和应用,让我们的 Linux 系统在网络安全的环境下稳定运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值