题比较简单,直接用求和公式求就行了,因为精度问题,需要几个特别判断,代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
int main()
{
ll n, t, Case = 1;
scanf("%lld", &t);
while (t --) {
scanf("%lld", &n);
ll a = (ll)sqrt(n * 2);
printf("Case #%lld: ", Case ++);
if (n == 2 || n == 1) {
printf("1\n");
}
else {
if (a * (a + 1) / 2 == n)
printf("%lld\n", n);
else if (a * (a + 1) / 2 < n)
printf("%lld\n", a * (a + 1) / 2);
else
printf("%lld\n", a * (a - 1) / 2);
}
}
return0;
}