Touch of Venom +SPOJ+推公式题

Touch of Venom

Problem code: VENOM


Sometimes you have to try fighting even though you know that your enemy is very powerful than you. Your hero with initial health H is about to fight against a venomous enemy who has a poisonous value of P. The enemy's poison deals i*P damage at it's ith attacking chance(i>=1). The hero dies when his health becomes <=0. After enemy's attack, if the hero survives, he heals himself with a health of A by using his skills. Then the enemy gets the chance again and the cycle continues till the hero dies. Find the survival time of the hero. You can safely assume that the hero is mortal.

Example Scenario:

Initial Health(H) = 10, Poison (P) = 2, Heal value(A) = 1

At time 1, enemy does 1*2 damage reducing the hero's health to 8

At time 2, hero heals himself by 1 increasing his health to 9

At time 3, enemy does 2*2 damage reducing the hero's health to 5

At time 4, hero heals himself by 1 increasing his health to 6

At time 5, enemy does 3*2 damage and kill the hero.

The hero survived 5 units of time.

Input:

The first line consists of an integer t, the number of test cases. For each test case there is a line with 3 integers H, P and A.

Output:

For each test case, find the survival time of the hero.

Input Constraints:

1<=t<=10^6

1<=H<=10^6

1<=P<=10^6

0<=A<P

Sample Input:

3

3 7 2

81 4 1

87 8 4

Sample Output:

1

13

9

解决方案:此题简单,可推出公式,但是要注意精度问题sqrt开方后的结果要减去0.000001。

code:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int H,P,A;
        scanf("%d%d%d",&H,&P,&A);
        double a=(P)/2.0;
        double b=(P/2.0-A);
        double c=A-H;
        double times1=(-1*b+sqrt((-4*a*c+(b*b))))/(2*a)-0.000001;
        int time=int(times1)+1;///注意要先取整后再加上1,不然若times1结果为负值时就会出现问题了。
        printf("%d\n",time*2-1);



    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值