n=int(input())
for e in range(n):
x=int(input())
if x==3:sum=1
else:
sum=0
for k in range(2,x//2+1):
if 2x%k==0:
if (2x/k-k+1)%2==0 and (2x/k-k+1)>0 and (2x/k-k+1)/2!=0:
sum+=1
print(‘Case {0}: {1}’.format(e+1,sum))
using namespace std;
const int MAX = 1000001;
long long int tot[MAX];
bool search(int, long long int);
int main(void)
{
for (long long int i = 1; i < MAX; i++)
tot[i] += tot[i - 1] + i;
int cnt;
cin >> cnt;
for (int index = 1; index <= cnt; index++)
{
int ret = 0;
long long int n;
cin >> n;
for (int i = 0; i <= n; i++)
if (search(i - 2, tot[i] - n))
ret++;
cout << “Case ” << index << “: ” << ret << endl;
}
return 0;
}
bool search(int line, long long int tar)
{
int left = 0, right = line;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (tot[mid] == tar)
return true;
else if (tot[mid] < tar)
left = mid + 1;
else if (tot[mid] > tar)
right = mid - 1;
}
return false;
}
题解加注释:
http://blog.csdn.net/kangyan__/article/details/72666912
n=int(input())
for e in range(n):
x=int(input())
if x==3:sum=1
else:
sum=0
for k in range(2,x//2+1):
if 2x%k==0:
if (2x/k-k+1)%2==0 and (2x/k-k+1)>0 and (2x/k-k+1)/2!=0:
sum+=1
print(‘Case {0}: {1}’.format(e+1,sum))
连续整数的和为(A(n+k)+An)(k+1)/2 = x,其中1<k<sqrt(2*x)
include
using namespace std;
const int MAX = 1000001;
long long int tot[MAX];
bool search(int, long long int);
int main(void)
{
for (long long int i = 1; i < MAX; i++)
tot[i] += tot[i - 1] + i;
int cnt;
cin >> cnt;
for (int index = 1; index <= cnt; index++)
{
int ret = 0;
long long int n;
cin >> n;
for (int i = 0; i <= n; i++)
if (search(i - 2, tot[i] - n))
ret++;
cout << “Case ” << index << “: ” << ret << endl;
}
return 0;
}
bool search(int line, long long int tar)
{
int left = 0, right = line;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (tot[mid] == tar)
return true;
else if (tot[mid] < tar)
left = mid + 1;
else if (tot[mid] > tar)
right = mid - 1;
}
return false;
}