zoukankan      html  css  js  c++  java
  • 51nod 1090 3个数和为0

    【题解】

      排序,然后枚举前两个数,再用类似two pointer的思想扫第三个数即可,不需要二分。复杂度n方。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #define LL long long
     6 #define rg register
     7 #define N 10010
     8 using namespace std;
     9 int n,tot,a[N];
    10 struct rec{
    11     int x,y,z;
    12 }b[N];
    13 inline int read(){
    14     int k=0,f=1; char c=getchar();
    15     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    16     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    17     return k*f;
    18 } 
    19 int main(){
    20     n=read();
    21     for(rg int i=1;i<=n;i++) a[i]=read();
    22     sort(a+1,a+1+n);
    23     for(rg int i=1;i<n-1;i++)
    24         for(rg int j=i+1;j<n;j++){
    25             int tmp=-a[i]-a[j],pointer=n;
    26             while(a[pointer]>tmp&&pointer>j) pointer--;
    27             if(a[pointer]==tmp&&pointer>j) b[++tot]=(rec){a[i],a[j],a[pointer]};
    28             if(pointer<=j) break; 
    29         }
    30     if(!tot){
    31         puts("No Solution"); return 0;
    32     }
    33     for(rg int i=1;i<=tot;i++) printf("%d %d %d
    ",b[i].x,b[i].y,b[i].z);
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    华为 简单OSPF实验
    华为 基于MAC地址的VLAN划分
    完全背包
    01背包问题
    90. 子集 II
    Java去除字符串中的特殊符号或者指定的字符
    Java查找指定文件夹下的所有文件
    Java面试基础
    Spring获取ApplicationContext
    JSP & EL & JSTL
  • 原文地址:https://www.cnblogs.com/DriverLao/p/9572763.html
Copyright © 2011-2022 走看看