pat 1029
来源:互联网 发布:ubuntu lightdm 配置 编辑:程序博客网 时间:2024/06/03 19:54
1、题目链接
https://www.patest.cn/contests/pat-a-practise/1029
2、题意分析
(1)题意:寻找一个序列数的中位数
(2)分析:使用two pointer方法来将两个数组合并,然后找出其中的中位数
3、题目坑点
(1)注意合并序列后,输出的数的下标为什么。(index-1)/2
4、源代码
#include <stdio.h>/*1.使用全局变量可以扩大数组范围 */#define max 1000001#define size 2000002long long array1[max],array2[max], array3[size]; //两个数组存放数据int main(){ int n1,n2, i ;//输入数组array1[] scanf("%d",&n1);for(i = 0;i<n1;i++){scanf("%ld",&array1[i]);} //输入数组array2[]的值 scanf("%d",&n2);for(i = 0;i < n2;i++){scanf("%ld",&array2[i]);} int j =0 ;long long index = 0;//index为array3[]的下标 //开始循环比较值 i = 0;while(i<n1 && j < n2){if(array1[i] < array2[j]){array3[index++] = array1[i++]; } else{array3[index++] = array2[j++];}}while(i < n1){array3[index++] = array1[i++];}while(j < n2){array3[index++] = array2[j++];}printf("index = %d\n",index);for(i = 0;i < index;i++){printf("%d ",array3[i]);}printf("\n");printf("%ld\n",array3[(index-1)/2]);}/*4 11 12 13 145 9 10 15 16 17211 12213 14**/
5、总结
(1)当然我们可以不比将两个数组中的数全部排序,我们只需要找到(n1+n2 - 1)/2这个地方即可。
代码如下:
#include <stdio.h>/*1.使用全局变量可以扩大数组范围 */#define max 1000001#define size 2000002long long array1[max],array2[max], array3[size]; //两个数组存放数据int main(){ int n1,n2, i ;//输入数组array1[] scanf("%d",&n1);for(i = 0;i<n1;i++){scanf("%ld",&array1[i]);} //输入数组array2[]的值 scanf("%d",&n2);for(i = 0;i < n2;i++){scanf("%ld",&array2[i]);} int j =0 ;long long index = 0;//index为array3[]的下标 //开始循环比较值 i = 0;while(i<n1 && j < n2){if(array1[i] < array2[j]){array3[index++] = array1[i++]; } else{array3[index++] = array2[j++];}if(index == ((n1 + n2 - 1) / 2) + 1){printf("%d",array3[index-1]);return 0;}}while(i < n1){array3[index++] = array1[i++];if(index == ((n1 + n2 - 1) / 2) + 1){printf("%d",array3[index-1]);return 0;}}while(j < n2){array3[index++] = array2[j++];if(index == ((n1 + n2 - 1) / 2) + 1){printf("%d",array3[index-1]);return 0;}}}/*4 11 12 13 145 9 10 15 16 17211 12213 14**/(2)或者我们也可以这么做,将这些数据全部写入到一个数组中,然后将这个数组排序,使用方法sort()即可,然后输出中间数。方法更加简单,代码如下:
#include <stdio.h>#include <algorithm>using namespace std;/*1.使用全局变量可以扩大数组范围 */#define size 2000002long long array[size]; //两个数组存放数据int main(){ int n1,n2, i, j =0 ;;scanf("%d",&n1);for(i = 0;i<n1;i++){scanf("%ld",&array[i]);} scanf("%d",&n2);for(j = i;j < n2 + i;j++){scanf("%ld",&array[j]);} sort(array,array+(n1+n2));printf("%d",array[(n1+n2-1)/2]);}/*4 11 12 13 145 9 10 15 16 17211 12213 14**/
0 0
- pat 1029
- pat 1029
- pat 1029
- PAT 1029
- PAT---1029
- pat 1029 Median
- PAT Advanced Level 1029
- PAT(A) 1029
- pat,1029Median
- PAT 1029 Median (25)
- PAT(甲级)1029
- 浙大PAT甲级 1029
- PAT A 1029
- PAT乙级1029
- pat-bl-1029
- PAT甲级1029
- pat乙级1029
- 浙大PAT甲级-1029
- Java通过axis调用WebService
- Android 日常开发总结的技术经验 60 条
- 警告错误整理(1)
- 查询Oracle日志中的SQL
- ExecutorService中submit和execute的区别
- pat 1029
- iOS学习笔记-007.猜图
- 下拉刷新ListView
- 润乾报表打印报错:invalid stream header的几种情况
- WEB_深入浅出 RPC - 深入篇
- Visual Studio 2012 和 SVN 结合实现版本控制 AnkhSvn
- Pow(x, n) / Super Pow
- gentoo on macbook pro driver for the Broadcom Facetime HD webcam
- @Table(name="problem",schema = "root") 的schema属性