一、求和
#include <iostream>
using namespace std;
int main()
{
int a[10],x=0,y=0,z=0;
cout<<"输入10个数:";
for(x=0;x<10;x++)
{
cin>>a[x];
if(a[x]>=0)
y+=a[x];
else
z+=a[x];
}
cout<<"正数之和为:"<<y;
cout<<"负数之和为:"<<z;
}
二、字符串
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[50];
int i=0,n=0,word=0,other=0;
cout<<"输入字符串:";
gets(str);
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9') n++;
else if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z') word++;
else other++;
i++;
}
cout<<"其中的数字个数是: "<<n;
cout<<"其中的字母个数是: "<<word;
cout<<"其他字符个数是: "<<other;
}
三、数组分离
#include<iostream>
using namespace std;
int main()
{
int a[10],b[10],c[10],num,x,y=0,z=0;
cout<<"请输入10个数放在数组a";
for(x=0;x<10;x++)
{
cout<<"第"<<x+1<<"个数为:";
cin>>num;
a[x]=num;
if(num%2==1)
{
b[y]=num;
y++;
}
else
{
c[z]=num;
z++;
}
}
cout<<"数组a是:";
for(x=0;x<10;x++)
cout<<a[x]<<", ";
cout<<"奇数数组b有";
for(x=0;x<y;x++)
cout<<b[x]<<", ";
cout<<endl;
cout<<"偶数数组c有";
for(x=0;x<z;x++)
cout<<c[x]<<", ";
}