1087 有多少不同的值

hash就完事了。

#include<iostream>
using namespace std;

bool hashtable[10010] = {false};
int main() {
    int n,cnt = 0;
    cin>>n;
    for(int i = 1; i <= n; ++i) {
        if(hashtable[i/2+i/3+i/5] == false) {
            hashtable[i/2+i/3+i/5] = true;
            cnt++;
        }
    }
    cout<<cnt;
    return 0;
}

 ps:使用STL中的set(自排序+去重)。代码如下。

 1 #include<iostream>
 2 #include<unordered_set>
 3 using namespace std;
 4 int main() {
 5     int n,cnt = 0;
 6     cin>>n;
 7     unordered_set<int> st;
 8     for(int i = 1; i <= n; ++i)
 9         st.insert(i/2+i/3+i/5);
10     cout<<st.size();
11     return 0;
12 }

 

posted @ 2020-02-26 10:08  tangq123  阅读(184)  评论(0编辑  收藏  举报