【PAT乙级】1087 有多少不同的值

当自然数 n 依次取 1、2、3、……、N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分。)

输入格式:

输入给出一个正整数 N(2≤N≤10​4​​)。

输出格式:

在一行中输出题面中算式取到的不同值的个数。

输入样例:

2017

输出样例:

1480

个人理解

这题可以设定一个bool型数组,记录某个n的和是否出现过,如果没有出现过,则计数+1,然后输出计数即可。

代码实现

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define ep 1e-5
#define INF 0x7FFFFFFF

const int maxn = 11000;

using namespace std;

int main() {
    // 输入
    int n;
    cin >> n;
    
    // 将出现的记录下来
    bool show_up[maxn];
    int cnt = 0;
    memset(show_up, 0, sizeof(show_up));
    for (int i = 1; i <= n; i ++) {
        int tmp = i/2 + i/3 + i/5;
        if (!show_up[tmp]) {
            show_up[tmp] = true;
            cnt ++;
        }
    }
    
    // 输出
    cout << cnt << endl;
    
    return 0;
}

总结

学习不息,继续加油

猜你喜欢

转载自blog.csdn.net/qq_34586921/article/details/83476574