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;

    return 0;

}

原文地址:https://www.cnblogs.com/guohaoyu110/p/6322917.html