题目链接
题意:姐姐从4岁开始向盒子里放与年数相等的蜡烛,弟弟从3岁开始,两人各有一个盒子,但是最近一些年弟弟把自己的蜡烛误放在姐姐的盒子里。现给出姐姐和弟弟的年龄差,以及现在姐姐盒子里和弟弟盒子里的蜡烛数量,求姐姐应该从自己的盒子里面拿出多少蜡烛才能得到当前应有的蜡烛。
思路:等差数列求和公式,然后进行模拟,如果某一年姐姐放的蜡烛和弟弟放的蜡烛之和等于题目给出的蜡烛之和,则找到那一年,然后用题目给出的减掉当年的就得到答案了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
using namespace std;
int main()
{
int d,r,t;
while(cin >> d >> r >> t)
{
int sum = t+r;//某一年可以到达这个数量
int x,y;//xy分别代表当前年数姐姐和弟弟的蜡烛数量
for(int i = 4; ; i++)
{
x = i*(i+1)/2-6;//这里手推一波公式
y = (i-d)*(i-d+1)/2-3;
if(x+y == sum)//到达之后就打破循环
break;
}
cout << r-x << endl;
}
return 0;
}