美文网首页工作生活
PAT 1083 是否存在相等的差 (20 分)

PAT 1083 是否存在相等的差 (20 分)

作者: 昭明ZMing | 来源:发表于2019-07-03 10:49 被阅读0次
    #include <iostream>
    #include<map>
    using namespace std;
    int main(){
        map<int, int, greater<int>> map;//第三个参数compare(默认为less<int>),此时我们需要降序
        int n, t;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> t;
            map[abs(t - i)]++;      //取绝对值加到map中
        }
        for (auto it = map.begin(); it != map.end(); it++)
            if(it->second>1)        //注意要重复次数大于1才输出
                cout << it->first << " " << it->second << endl;
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PAT 1083 是否存在相等的差 (20 分)

        本文链接:https://www.haomeiwen.com/subject/gvtqhctx.html