有N只牛,编号为1, 2, 3 ... N,问取其中几头牛,有几种取法使取出的牛的编号和为N。
只要考虑取出的牛的头数(M),从1开始迭代,若能使编号和为N,那么 N - (1 + 2 + ... + M) 必然能被 M 整除。
#include<iostream>
using namespace std;
int main(){
int n;
int result = 0;
cin >> n;
for(int i = 1; (i + 1) * i / 2 <= n; i++){
int temp = (i + 1) * i / 2;
if((n - temp) % i == 0)
result++;
}
cout << result << endl;
}