编程题:将奇数数组与偶数数组合并为一个数组
偶数 -> 0 2 4 6 8 奇数 -> 1 3 5 7 9 合并后 -> 0 2 4 6 8 1 3 5 7 9
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define N 10 int main(void) { int array[N],odd[N/2]={ 1, 3, 5, 7, 9 }, even[N / 2]={ 2,4, 6,8,0 }, t=0;//奇数数组 偶数数组 puts("奇数数组为:"); for (int i = 0; i < N / 2; i++) { printf("%d ", odd[i]); } printf("\n"); puts("偶数数组为:"); for (int i = 0; i < N/2; i++) { printf("%d ", even[i]); } int i ; for (i= 0; i < N/2; i++) { array[i] = even[i]; } for (i; i < N; i++) { array[i] = odd[t]; t++; } puts("合并数组为:"); for (int i = 0; i < N; i++) { printf("%d ", array[i]); } return 0; }
#include<iostream>
using namespace std;
void copy(int * array0,int length0,int * array1,int length1,int *start0,int * start1)
{
if(length0==0 && length1==0)
return;
}
if(length0>0)
*start0=*array0;
if(length1>0)
*start1=*array1;
copy(array0+1,length0-1,array1+1,length1-1,start0+1,start1+1);
void traverseArray(int * array,int length)
if(length==1)
cout<<*array<<endl;
cout<<*array<<",";
traverseArray(array+1,length-1);
int main()
int evenArray[]={0,2,4,6,8};
int oddArray[]={1,3,5,7,9};
int length1=sizeof(evenArray)/sizeof(int);
int length2=sizeof(oddArray)/sizeof(int);
cout<<"偶数组"<<endl;
traverseArray(evenArray,length1);
cout<<"奇数组"<<endl;
traverseArray(oddArray,length2);
int length=length1+length2;
int * newborn=new int[length];
copy(evenArray,length1,oddArray,length2,newborn,newborn+length1);
cout<<"合并后"<<endl;
traverseArray(newborn,length1+length2);
return 0;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> int main() { int array[10]; int even[5] = {0, 2, 4, 6, 8}; int odd[5] = {1, 3, 5, 7, 9}; int loop, index, e_len, o_len; e_len = o_len = 5; index = 0; for(loop = 0; loop < e_len; loop++) { array[index] = even[loop]; index++; } for(loop = 0; loop < o_len; loop++) { array[index] = odd[loop]; index++; } printf("\n偶数 -> "); for(loop = 0; loop < e_len; loop++) printf(" %d", even[loop]); printf("\n奇数 -> "); for(loop = 0; loop < o_len; loop++) printf(" %d", odd[loop]); printf("\n合并后 -> "); for(loop = 0; loop < 10; loop++) printf(" %d", array[loop]); return 0; }
登录后提交答案