编程题:将一个数组拆分为一个为奇数数组,一个为偶数数组
原始数组 -> 0 1 2 3 4 5 6 7 8 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] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 },odd[N],even[N],o=0,e=0;//奇数数组 偶数数组 奇数下标 偶数下标 puts("原数组"); for (int i = 0; i < N; i++) { if ( array[i]%2==0) {//%2是否为0判断奇偶数 even[e]= array[i]; e++; }else{ odd[o] = array[i]; o++; } printf("%d ", array[i]); } printf("\n"); puts("奇数数组为:"); for (int i = 0; i < o; i++) { printf("%d ", odd[i]); } printf("\n"); puts("偶数数组为:"); for (int i = 0; i < e; i++) { printf("%d ", even[i]); } return 0; }
#include<iostream>
using namespace std;
void traverseArray(int * array,int length)
{
if(length==1)
cout<<*array<<endl;
return;
}
cout<<*array<<",";
traverseArray(array+1,length-1);
int main()
int array[]={0,1,2,3,4,5,6,7,8,9};
int length=sizeof(array)/sizeof(int);
int evenArray[length/2]={0};
int oddArray[length/2]={0};
int * p1=evenArray;
int * p2=oddArray;
for(int i=0;i<length;i++)
if(i[array]%2)
*p2++=i[array];
}else
*p1++=i[array];
cout<<"original array->\t";
traverseArray(array,length);
cout<<"even array->\t";
traverseArray(evenArray,length/2);
cout<<"odd array->\t";
traverseArray(oddArray,length/2);
return 0;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> int main() { int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int even[10], odd[10]; int loop, e, d; e = d = 0; for(loop = 0; loop < 10; loop++) { if(array[loop]%2 == 0) { even[e] = array[loop]; e++; }else { odd[d] = array[loop]; d++; } } printf(" 原始数组 -> "); for(loop = 0; loop < 10; loop++) printf(" %d", array[loop]); printf("\n 偶数 -> "); for(loop = 0; loop < e; loop++) printf(" %d", even[loop]); printf("\n 奇数 -> "); for(loop = 0; loop < d; loop++) printf(" %d", odd[loop]); return 0; }
登录后提交答案