项目2 数组选择
#include<iostream> using namespace std; int main() { const int N=10; int A[N],B[N],i,j,k=0; cout<<"从键盘中输入十个数"<<endl; for(i=0;i<N;i++) cin>>A[i]; for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(i==j) continue; if(A[i]==A[j]) break; if(j==N-1) { B[k]=A[i]; k++; } } } cout<<"数组B为:"; for(i=0;i<k;i++) cout<<B[i]<<" "; return 0; }
项目3——数组求和
#include <iostream> using namespace std; int main() { int i,a[10],sum1=0,sum2=0; cout<<"输入十个数:"; for(i=0;i<10;i++) { cin>>a[i]; } for(i=0;i<10;i++) { if(a[i]>0) sum1+=a[i]; else if(a[i]<0) sum2+=a[i]; } cout<<"sum1="<<sum1<<endl; cout<<"sum2="<<sum2<<endl; return 0; }
四。
#include<iostream> using namespace std; int main() { int N, i = 0, j = 0, k = 0,m, n; double a[10], max = 0, min = 0, sum = 0, average = 0; cout<< "请输入10个同学的成绩(0到100之间):"; for (i = 0; i < 10; i++) { cin >> a[i]; } max = a[0]; min = a[0]; for (i = 1; i<10; i++) { if (a[i]>max) max = a[i]; if (a[i]<min) min = a[i]; } cout << "最高分为" << max << "," << "最低分为" << min << ","; for (i = 0; i<10; i++) { sum = sum + a[i]; } average = sum / 10.0; cout << "平均分为" << average << endl; cout << "最高分同学的学号为"; for (i = 0; i<10; i++) { if (max == a[i]) { j++; m= i+1; cout << m << " "; } } cout << endl; cout << "最低分同学的学号为"; for (i = 0; i < 10; i++) { if (min == a[i]) { k++; n = i + 1; cout << n <<" "; } } cout << endl; cout << "分数最高的同学有" <<j<< endl; cout << "分数最低的同学有" <<k<< endl; return 0; }
时间: 2024-11-05 11:37:57