zoukankan      html  css  js  c++  java
  • PAT 1083 是否存在相等的差

    给定 N 张卡片,正面分别写上 1、2、……、N,然后全部翻面,洗牌,在背面分别写上 1、2、……、N。将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差?

    输入格式:
    输入第一行给出一个正整数 N(2 ≤ N ≤ 10 000),随后一行给出 1 到 N 的一个洗牌后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字。

    输出格式:
    按照“差值 重复次数”的格式从大到小输出重复的差值及其重复的次数,每行输出一个结果。

    输入样例:

    8
    3 5 8 6 2 1 4 7
    

    输出样例:

    5 2
    3 3
    2 2
    
    #include<iostream>
    #include<map>
    #include<math.h>
    using namespace std;
    int main(){
      int N;
      cin>>N;
      map<int, int> count;
      for(int i=1; i<=N; i++){
        int num;
        cin>>num;
        count[abs(num-i)]++;
      }
      auto last = count.end();
      last--;
      for(;last!=count.begin();last--)
        if(last->second!=1)
          cout<<last->first<<" "<<last->second<<endl;
      if(count.begin()->second!=1)
      cout<<count.begin()->first<<" "<<count.begin()->second<<endl;
      return 0;
    }
    
  • 相关阅读:
    Ext.FormPanel-----FieldSet的用法
    DAO层,Service层,Controller层、View层
    PageProxy分页的实现
    Layout布局(补充)
    Ext--Layout(布局)
    DirectEvents用法
    Linq的使用
    字符串注入攻击
    winform(C#)里弹出“确定”“取消”对话框
    C#的数组
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/9833463.html
Copyright © 2011-2022 走看看