Algoogle

Algorithm for Programming Contest

PKU 2140 Herd Sums

Category: PKU Tag: inchworm-method

Herd Sums

問題概要


解法


連続した数の和なのでしゃくとりでやる.

コード


(2140.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>

using namespace std;

int N, ans;

int main(){
    scanf("%d", &N);
    int l = 1, r = 2, sum = 1;
    while(l <= N){
        if(sum == N){
            ans++;
            sum -= l;
            l++;
        }
        else if(sum < N){
            sum += r;
            r++;
        }
        else {
            sum -= l;
            l++;
        }
    }
    printf("%d\n", ans);
    return 0;
}

Comments