poj 2140 Herd Sums

#include <iostream>
using namespace std;


int main()
{
    int n, i, j, sum, cnt = 0;
    cin >> n;
    //经分析可知,其i的取值不可能大于n/2+1,那样范围就缩小了! 
    for (i = 1; i <= n/2+1; i++){
        sum = 0;
        for (j = i; j <= n/2+1; j++){
            sum += j;
            if (sum == n){
                cnt++;
                break;
            }
            if (sum > n)  break;
        }
    }
    cout << cnt + 1 << endl;
    
    system("pause");
}

你可能感兴趣的:(System,include)