zoukankan      html  css  js  c++  java
  • 51nod 1001 数组中和等于k的数对

    给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对。例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0,8),(2,6),(3,5)。
    Input
    第1行:用空格隔开的2个数,K N,N为A数组的长度。(2 <= N <= 50000,-10^9 <= K <= 10^9)
    第2 - N + 1行:A数组的N个元素。(-10^9 <= A[i] <= 10^9) 
    Output
    第1 - M行:每行2个数,要求较小的数在前面,并且这M个数对按照较小的数升序排列。
    如果不存在任何一组解则输出:No Solution。
    Input示例
    8 9
    -1
    6
    5
    3
    4
    2
    9
    0
    8
    Output示例
    -1 9
    0 8
    2 6
    3 5


    set或者二分,水题。
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <set>
     5 using namespace std;
     6 const int MAX = 50010;
     7 int a[MAX], k, n;
     8 set<int> st, st1;
     9 
    10 int main() {
    11     cin>>k>>n;
    12     for(int i = 1; i <= n; i ++) scanf("%d",&a[i]),st.insert(a[i]);
    13     sort(a+1, a+1+n);
    14     int ans = 0;
    15     for(int i = 1; i <= n; i ++) {
    16         int x = k - a[i];
    17         if(st.count(x) && x != a[i] && !st1.count(x))
    18         printf("%d %d
    ",a[i], x), st1.insert(a[i]),st1.insert(x), ans++;
    19     }
    20     if(ans == 0)
    21         printf("No Solution
    ");
    22     return 0;
    23 }
  • 相关阅读:
    [转]system函数返回值探究
    [转]bat中的特殊字符,以及需要在bat中当做字符如何处理
    [转]null和""以及==与equals的区别
    粘包问题
    并发编程
    GIL锁
    五种IO模型
    css选择器
    并发与串行
    模块(二)
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7275008.html
Copyright © 2011-2022 走看看