统计一个数组中正数和负数的个数

1.遍历数组,将正数重新放在一个数组中,负数放在另一个数组中

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a[5],i,j=0,k=0;
 6     int s1[5],s2[5];
 7     cout<<"Please enter 5 numbers"<<endl;
 8     for(i=0;i<5;i++)
 9         cin>>a[i];
10     for(i=0;i<5;i++)
11     { 
12         if(a[i]>0)
13         s1[j++]=a[i];     
14         else if(a[i]<0)
15          s2[k++]=a[i];
16     }
17     cout<<"The positive numbers are:"<<endl;
18     for(i=0;i<j;i++)
19     cout<<s1[i]<<" ";
20     cout<<endl;
21     cout<<"The negative numbers are:"<<endl;
22     for(i=0;i<k;i++)   
23     cout<<s2[i]<<" ";
24     cout<<endl;
25     system("pause");
26     return 0;
27 }

2.显示结果

原文地址:https://www.cnblogs.com/dongyanxia1000/p/4905411.html