给定一个10长度的数组a,数组每个元素赋随机值(随机值范围是0-100)
另外给出两个数组b、c。将数组a中奇数放入数组b中 偶数放入数组c中
例:数组a 100 65 34 0 32 13 42 65 64 39 20 45 65
输出 数组b 65 13 65 39 45 65
数组c 100 34 0 32 42 64 20
#include<iostream>
#include<ctime>
using namespace std;
int main(){
int a[10],b[10],c[10],ib=0,ic=0;
srand(time(NULL));
for(int i=0;i<10;i++) {
a[i] = rand() % 101;
if(a[i]%2==1)b[ib++]=a[i];
else c[ic++]=a[i];
}
cout<<"数组b";
for(int i=0;i<ib;i++) cout<<" "<<b[i];
cout<<endl;
cout<<"数组c";
for(int i=0;i<ic;i++) cout<<" "<<c[i];
return 0;
}
按照题目要求编写的C++程序如下
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int a[10],b[10],c[10],i,j=0,k=0;
srand(time(NULL));
for(i=0;i<10;i++){
a[i]=rand()%101;
if(a[i]%2==1)
b[j++]=a[i];
else
c[k++]=a[i];
}
cout<<"数组a ";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"数组b ";
for(i=0;i<j;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<"数组c ";
for(i=0;i<k;i++)
cout<<c[i]<<" ";
return 0;
}