Codeforces Gym 101174 C. Candle Box

来源:互联网 发布:淘宝开店成功案例 编辑:程序博客网 时间:2024/06/15 09:36

Problem

给出 RitaTheo 的年龄差 D (age[Rita] - age[Theo] = D)以及 Rita 的蜡烛数量 R, Theo 的蜡烛数量 T 。

已知 Rita 从 4 岁起、Theo 从 3 岁起(包括),每年获得等同于年龄数量的蜡烛。

Theo 将他的一些蜡烛给了 Rita 。问给了 Rita 的具体数量

限制条件

1D20

4R<1000

0T<1000

解题思路

模拟 Rita 的年龄为 age ,统计 Rita 能获得的总蜡烛数 correct,和两人的总蜡烛数 candles ,若 candles == R+T ,则输出 R-correct

代码

#include<bits/stdc++.h>using namespace std;int main(){    int d, r, t, candles = 0, correct = 0;    scanf("%d %d %d", &d, &r, &t);    for(int age=1;age<=2000;age++)    {        if(age >= 4)    candles+=age,   correct += age;        if(age-d >= 3)  candles+=age-d;        if(r+t == candles) {            printf("%d\n", r - correct);            return 0;        }    }}