1001 数组中等于K的数对
来源:互联网 发布:dayz独立版垃圾优化 编辑:程序博客网 时间:2024/05/20 18:43
#include<iostream>#include<vector>#include<algorithm>using namespace std;int A[50000];int main(){int N,i,j;int flag=0; long long K; cin>>K;cin>>N; for(i=0;i<N;i++){scanf("%d",&A[i]); } sort(A,A+N); for(i=0,j=N-1;i<j;i++){if((A[i]+A[j])>K){j--;i--;}else{if((A[i]+A[j])==K){flag=1;cout<<A[i]<<" "<<A[j]<<endl;}} } if(flag==0){ cout<<"No Solution";} return 0;}
错误总结:
最开始写了个暴力超时,需要改为O(n)才行 (最开始的sort为n*logn)
sort(A,A+N);传入参数为地址,起始位A,排序N个,结尾本应为A+N-1,但这里的参数应传入A+N 多一个
j=N-1写成了j==N-1。。。
0 0
- 1001 数组中等于K的数对
- 查找有序数组中等于给定值的两个数
- 求子数组中等于某个值K的起始坐标
- 51nod 1001 数组中和等于K的数对
- 51Nod 1001 数组中和等于K的数对
- 51Nod 1001 数组中和等于K的数对
- 51nod 1001 数组中和等于K的数对
- 51nod 1001 数组中和等于K的数对
- 1001数组中和等于k的数对
- 51nod-1001 数组中和等于K的数对
- 51nod-【1001 数组中和等于K的数对】
- 51nod 1001 数组中和等于K的数对
- 51node 1001 数组中和等于K的数对
- 51nod 1001 数组中和等于K的数对
- 51NOD 1001 数组中和等于K的数对
- 51nod 1001 数组中和等于K的数对
- 51Nod 1001 数组中和等于K的数对
- 【51Nod】1001 数组中和等于K的数对
- Firefox的安装和配置
- eclipse 下 resin 配置
- UEFI原理与编程(二):UEFI工程模块文件-标准应用程序工程模块
- 黑客攻防之SQL注入原理解析入门教程
- Spring Boot与Quartz集成
- 1001 数组中等于K的数对
- centos 7.0 下编辑安装php5.6
- 启动HBase抛出org.apache.hadoop.hbase.ClockOutOfSyncException异常
- Android 注解 ButterKnife Zelezny插件的安装与使用
- 自己动手写个聊天机器人吧
- mysql 环境
- linux 下使用shadowsocks代理服务
- [Leetcode] 114. Flatten Binary Tree to Linked List 解题报告
- SAP特性,物料特性,批次特性建立的BAPI函數