编写函数,将数组中的所有奇数存放到另一个数组中,输出两个数组。。

如题所述

第1个回答  2012-06-18
#include <iostream>
using namespace std;
int main()
{
cout<<"请输入数字,并输入“-1”表示结束:"<<endl;
int a[1005];
int b[1005];
int i=0;
while(cin>>a[i],a[i]!=-1)
{
i++;
//cout<<"*"<<i<<endl;
}
int r=0;
for(int j=0;j<=i;j++)
{
if(a[j]%2==1)
{
b[r]=a[j];
//cout<<"*"<<b[r]<<endl;
r++;
}
}

for(int z=0;z<i;z++)
cout<<a[z]<<" ";
cout<<endl;
for(int t=0;t<r;t++)
cout<<b[t]<<" ";

return 0;
}
第2个回答  2012-06-18
#include<stdio.h>

void main()
{
int a[20];
int b[20];
int c[20];
int i,j = 0;
int n;
int e,d = 0;
printf("please input 20 number\n");
for(i = 0; i < 20; i++)
scanf("%d",&a[i]);
for(i = 0; i < 20; i++)
{
if(a[i] % 2 == 1)
{
b[j] = a[i];
j++;
}
else
{
c[d] = a[i];
d++;
}
}
printf("偶数数组\n");
for(e = 0; e < d ; e++)
printf("%-3d",c[e]);
printf("\n奇数数组\n");
for(n = 0; n < j; n++)
printf("%-3d",b[n]);
printf("\n");
}

运行结果
please input 20 number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
偶数数组
2 4 6 8 10 12 14 16 18 20
奇数数组
1 3 5 7 9 11 13 15 17 19
Press any key to continue本回答被提问者和网友采纳
相似回答