标签:
项目1 数组分离
#include <iostream> using namespace std; void main() { int a[10],b[10],c[10],i=0,j=0,k=0; cout<<"请输入十个数"<<endl; for(i=0;i<10;i++) { cin>>a[i]; if((a[i]%2)==0) { b[j]=a[i]; j++; } else { c[k]=a[i]; k++; } } for(i=0;i<j;i++) cout<<"偶数数组为:"<<b[i]<<endl; for(i=0;i<k;i++) cout<<"奇数数组为:"<<c[i]<<endl; }
#include<iostream> using namespace std; void main() { const int M=10; int score[M],i=0,j=0,k=0,min=100,max=0,sum=0; double average; cout<<"请输入十位同学的成绩:"<<endl; for(i=0;i<M;i++) cin>>score[i]; for(i=0;i<M;i++) { if(score[i]<0||score[i]>100) cout<<"输入的成绩有误,请重新输入"<<endl; else sum=sum+score[i]; } average=sum/10.0; cout<<"这十名同学的平均成绩为:"<<average<<endl; for(i=0;i<M;i++) { if(score[i]>max) max=score[i]; if(score[i]<min) min=score[i]; } cout<<"最高成绩为:"<<max<<endl; cout<<"最低成绩为:"<<min<<endl; for(i=0;i<M;i++) { if(score[i]==max) { cout<<"获得最高成绩同学的学号为:"<<i+1<<endl; j++; } if(score[i]==min) { cout<<"获得最低成绩同学的学号为:"<<i+1<<endl; k++; } } cout<<"获得最高成绩的同学的个数为:"<<j<<"个"<<endl; cout<<"获得最低成绩的同学的个数为:"<<k<<"个"<<endl; }
#include<iostream> #include<cstdio> using namespace std; int main() { char str[50]; int i=0,n=0,j=0,k=0; cout<<"输入字符串:"; gets(str); while(str[i]!='\0') { if(str[i]>='0'&&str[i]<='9') { n++; i++; } else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') { j++; i++; } else { k++; i++; } } cout<<"其中的数字个数是: "<<n<<endl; cout<<"其中的字母的个数是:"<<j<<endl; cout<<"其中的其他字符的个数是:"<<k<<endl; return 0; }
标签:
原文地址:http://blog.csdn.net/zengqingpeng1997m/article/details/51359962