第一行两个整数,表示两个数组的长度, 接下来两行表示两个数字的值, 最后一行有四个整数a,b,c,d。 数组长度不会超过1000000。
每行一个整数,对应合并数组的下标在中间的那个值。
5 4 1 2 3 4 5 6 7 8 9 1 2 1 3
6
#include <stdio.h> #define N 1000000 int buf[N], buf1[N], buf2[N]; int main() { int a, b, c, d; int size, size1, size2; while(scanf("%d %d", &size1, &size2)!=EOF) { for(int i=0; i<size1; i++) { scanf("%d", &buf1[i]); } for(int i=0; i<size2; i++) { scanf("%d", &buf2[i]); } scanf("%d %d %d %d", &a, &b, &c, &d); size=0; for(int i=a-1; i<=b-1; i++) { buf[size++]=buf1[i]; } for(int i=c-1; i<=d-1; i++) { buf[size++]=buf2[i]; } printf("%d\n", buf[(size-1)/2]); } }
while True:
try:
a=int(input())
for i in range(a):
b=input()
c=list(map(int,input().split()))
d=list(map(int,input().split()))
num1,num2,num3,num4=map(int,input().split())
res=c[num1-1:num2]+d[num3-1:num4]
length=len(res)
print(res[length//2-1] if length%2==0 else res[length//2])
except:
break
#include<iostream> using namespace std; int main() { int m,n,a,b,c,d; while(cin>>m>>n) { int num[m+n]; for(int i=0;i<m+n;cin>>num[i++]); cin>>a>>b>>c>>d; int len=b+d-a-c+2; int me[len]; for(int i=0,j=a-1;i<len;j==b ? j=m+c-1:me[i++]=num[j++]); cout<<me[(len-1)/2]<<endl; } }
#include<stdio.h>//1.放到同一数组 2.取中值 3.下标从1开始更方便 int main() { int m,n,x[1000000],y[1000000],z[1000000],w=0,i,j,a,b,c,d; scanf("%d%d",&m,&n);//输入 for(i=1;i<=m;i++) scanf("%d",&x[i]);//下标从1开始输入 for(i=1;i<=n;i++) scanf("%d",&y[i]); scanf("%d%d%d%d",&a,&b,&c,&d); for(i=a;i<=b;i++)//取出放到新数组 { z[w]=x[i];//下标从0开始 w++;//到最后一共w个数 下标0---w-1 } for(i=c;i<=d;i++) { z[w]=y[i]; w++; } printf("%d",z[(w-1)/2]); }
#include<iostream> using namespace std; int main(){ int size1,size2; while(cin>>size1>>size2){ int* ch1=new int[size1]; int* ch2=new int[size2]; for(int i=0;i<size1;i++) cin>>ch1[i]; for(int i=0;i<size2;i++) cin>>ch2[i]; int a,b,c,d; cin>>a>>b>>c>>d; int size3=b-a+d-c+2,count3=0; int* ch3=new int[size3]; for(int i=a-1;i<=b-1;i++) ch3[count3++]=ch1[i]; for(int i=c-1;i<=d-1;i++) ch3[count3++]=ch2[i]; if(count3%2==0) cout<<ch3[count3/2-1]<<endl; else cout<<ch3[count3/2]<<endl; } }
while True: try: nums = list(map(int,input().split())) array1 = list(map(int,input().split())) array2 = list(map(int,input().split())) a,b,c,d = list(map(int,input().split())) newArray = array1[a-1:b]+array2[c-1:d] #第一个是下标0 print(newArray[(len(newArray)-1)//2]) #取下标小的直接长度减一后除二 except Exception: break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int m = scanner.nextInt(); int n = scanner.nextInt(); int[] arr1 = new int[m]; int[] arr2 = new int[n]; for (int i = 0; i < arr1.length; i++) { arr1[i] = scanner.nextInt(); } for (int i = 0; i < arr2.length; i++) { arr2[i] = scanner.nextInt(); } int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int d = scanner.nextInt(); int[] arr = new int[b-a+1 + d-c+1]; for (int i = 0; i < arr.length; i++) { if (i < b-a+1) { arr[i] = arr1[a+i-1]; }else { arr[i] = arr2[c-1 + i - b+a-1]; } } if (arr.length % 2 == 0) { System.out.println(arr[arr.length/2-1]); }else { System.out.println(arr[arr.length/2]); } } } }
#include <iostream> #include <vector> using namespace std; int main() { int n1, n2, a, b, c, d; while (cin >> n1 >> n2) { vector<int>arr1, arr2; arr1.push_back(0); //填充arr1[0] arr2.push_back(0); //填充arr2[0] while (n1--) { int data; cin >> data; arr1.push_back(data); } while (n2--) { int data; cin >> data; arr2.push_back(data); } cin >> a >> b >> c >> d; vector<int>merge; //合并后数组 for (int i = a; i <= b; i++) { merge.push_back(arr1[i]); } for (int i = c; i <= d; i++) { merge.push_back(arr2[i]); } cout << merge[(merge.size() - 1) / 2] << endl; } return 0; }
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { int a, b; cin >> a >> b; vector<int> va(a), vb(b); for (int i = 0; i < a; i++) { cin >> va[i]; } for (int i = 0; i < b; i++) { cin >> vb[i]; } int a1, a2, b1, b2; cin >> a1 >> a2 >> b1 >> b2; vector<int> ans(a2 - a1 + 1 + b2 - b1 + 1); move(va.begin() + a1 - 1, va.begin() + a2, ans.begin()); move_backward(vb.begin() + b1 - 1, vb.begin() + b2, ans.end()); cout << ans[(ans.size() - 1) / 2] << endl; }
#include <iostream> #include<vector> #include<algorithm> using namespace std; int main() { int a, b; while(cin >> a >> b){ vector<int>A(a); vector<int>B(b); for (int i = 0; i < a; i++) { cin >> A[i]; } for (int i = 0; i < b; i++) { cin >> B[i]; } int beg1, end1, beg2, end2; cin >> beg1 >> end1; cin >> beg2 >> end2; int len1=end1-beg1+1; int len2=end2-beg2+1; int len=len1+len2; vector<int>res(len); for(int i=0;i<len1;i++){ res[i]=A[beg1+i-1]; } for(int i=0;i<len2;i++){ res[len1+i]=B[beg2+i-1]; } if(len%2==0) cout<<res[len/2-1]; else cout<<res[len/2]; } }
#include <cstdio> #include <algorithm> using namespace std; int main(){ int m,n; while(scanf("%d %d",&m,&n) != EOF){ int arr1[m]; int arr2[n]; for(int i = 0; i < m; ++i){ scanf("%d",&arr1[i]); } for(int i = 0; i < n; ++i){ scanf("%d",&arr2[i]); } int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d); int len = b-a+1 + d-c+1; //总长度为 b-a+1 + d-c+1 int arr3[len]; int s1 = a-1,s2 = c-1; for(int i = 0; i < b-a+1; ++i){ arr3[i] = arr1[s1]; s1++; } for(int i = b-a+1; i < len; ++i){ arr3[i] = arr2[s2]; s2++; } // sort(arr3,arr3+len); //这题是取中间那个值,没有要求排序!!! printf("%d\n",arr3[(len+1)/2-1]); } return 0; }
#include <iostream> #include <algorithm> using namespace std; int main() { int q1[1000020],q2[1000020]; long int as,bs,a,b,c,d; while(cin>>as>>bs){//先将输入都保存下来 for(long int i=1;i<=as;i++){ cin>>q1[i]; } for(int i=1;i<=bs;i++){ cin>>q2[i]; } cin>>a>>b>>c>>d; //之前以为中间数是按从小到大输出数值中间的,原来只是输出下标在中间的 //sort(q1+a,q1+b); //sort(q2+c,q2+d); int q1index=a,q2index=c,count=0,temp;//count用来记目前是第几个元素,temp用来保存值 while(count<(b-a+3+d-c)/2){ if(q1index>b){//先遍历第一个数组,第一个数组遍历完再遍历第二个 temp=q2[q2index]; q2index++; count++; } else{ temp=q1[q1index]; q1index++; count++; } } cout<<temp<<endl; } }
#include <cstdio> #include <iostream> #include <vector> using namespace std; int main() { int a,b; while(scanf("%d%d",&a,&b)!=-1){ vector<int> arr,brr,crr; for(int i=0;i<a;i++){ arr.push_back(0); cin>>arr[i]; } for(int i=0;i<b;i++){ brr.push_back(0); cin>>brr[i]; } int l,m,n,o; cin>>l>>m>>n>>o; for(int i=l-1;i<=m-1;i++){ crr.push_back(arr[i]); } for(int i=n-1;i<=o-1;i++){ crr.push_back(brr[i]); } // sort(crr.begin(),crr.end()); if(crr.size()%2==0){ cout<<crr[crr.size()/2-1]<<endl; }else{ cout<<crr[crr.size()/2]<<endl; } } }
#include<stdio.h> #include<algorithm> using namespace std; int main(){ int n1,n2; scanf("%d %d",&n1,&n2); int *num1=(int *)malloc(sizeof(int)*n1); int *num2=(int *)malloc(sizeof(int)*n2); for(int i=0;i<n1;i++){ int temp1; scanf("%d",&temp1); num1[i]=temp1; } for(int j=0;j<n2;j++){ int temp2; scanf("%d",&temp2); num2[j]=temp2; } int n1_a,n1_b,n2_c,n2_d; scanf("%d %d %d %d",&n1_a,&n1_b,&n2_c,&n2_d); int n=n1_b+n2_d-n1_a-n2_c+2; int *num3=(int *)malloc(sizeof(int)*n); int k=0; //num3的指针 for(int i=n1_a-1;i<=n1_b-1;i++){ num3[k]=num1[i]; k++; } for(int j=n2_c-1;j<=n2_d-1;j++){ num3[k]=num2[j]; k++; } //sort(num3,num3+n); printf("%d",num3[(n-1)/2]); printf("\n"); return 0; }
#include<stdio.h> int main() { int m,n,i,j,k,i_2; int a,b,c,d; int a_m[10000]={0},a_n[1000]={0},a_sum[10000]={0}; while (scanf("%d %d",&m,&n)!=EOF) { //输入数组 for(i=1;i<=m;i++) scanf("%d",&a_m[i]); for(i=1;i<=n;i++) scanf("%d",&a_n[i]); //输入合并数 scanf("%d %d %d %d",&a,&b,&c,&d); //合并 j=1; for(i=a;i<=b;i++) { a_sum[j]=a_m[i]; j++; } i=0; for(i=c;i<=d;i++) { a_sum[j]=a_n[i]; j++; } //判断奇偶 i_2=b-a+1+d-c+1; if(i_2%2==0)//偶数 { i_2=i_2/2; } else//奇数 { i_2=i_2/2+1; } //输出 printf("%d\n",a_sum[i_2]); } return 0; }
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int N=10010; int a[N]; int b[N]; vector<int>ve; int main(void) { int n1,n2; cin>>n1>>n2; for(int i=0;i<n1;i++) cin>>a[i]; for(int i=0;i<n2;i++) cin>>b[i]; int start1,end1; int start2,end2; cin>>start1>>end1; cin>>start2>>end2; for(int i=start1-1;i<end1;i++) ve.push_back(a[i]); for(int i=start2-1;i<end2;i++) ve.push_back(b[i]); if(ve.size()%2==0) cout<<ve[ve.size()/2-1]<<endl; else cout<<ve[ve.size()/2]<<endl; return 0; }