PATB1087 how many different topics logic values can be used to improve search efficiency and thought hand

Title Description

When the natural number n sequentially capturing l, 2,3, ......, N , the formula ⌊ n / 2⌋ ⌊ + n / 3⌋ + ⌊ n / 5⌋ how many different values are? (Note: ⌊ x ⌋ is the floor function, represents no more than x the maximum natural number, i.e., x integer part.)

Input formats:

Input gives a positive integer N (2 ≦ N ≤104).

Output formats:

The number of different values ​​of the output face formula title in a row to take.

Sample input:

2017

Sample output:

1480

analysis

Treatment: in fact, an array of fixed length, variable length arrays may be used in the question head; because the number actually is constant.

Thinking: variable-length array stored value v calculated; it is determined whether the cycle is to contrast appeared, and it is equal its predecessor, if equal cnt is decremented by 1. To tell the truth is not very efficient, to a binary search?

Code

#include <iostream>
#include <vector>
int main(){
    int N, cnt = 0, tmp = 0;
    std::vector<int> v;
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) {
         tmp = (i / 2) + (i / 3) + (i / 5);
         v.push_back(tmp);
         cnt++;
    }
    for (int i = 2; i <= v.size(); i++) {
         if (v[i] == v[i - 1]) cnt--;
    }
    std::cout << cnt;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_40677350/article/details/91420733