第1个回答 2009-06-15
#include <iostream>
using namespace std;
void bubbleSort(int* score,int len);
void printSort(int* score,int len);
int main()
{
int score[11];
int iscore;
int i = 0;
cout << "please input 10 scores:\n";
for (i = 0; i < 10; i++)
{
cin >> iscore;
if(!cin.fail())
{
score[i] = iscore;
}
else
{
cout << "the input is error,please input correct number.\n";
return 1;
}
}
bubbleSort(score,10); //十名学生成绩排序
printSort(score,10); //输出十名学生排序后的成绩
cout << "please input 1 score:\n";
cin >> iscore;
if(cin.fail())
{
cout << "the input is error,please input correct number.\n";
return 1;
}
score[10] = 0; //初始化最后一个值为0
//插入已经排好的序列中
for(i = 0; i < 11; i++)
{
if (score[i] < iscore)
{
for(int j = 10; j > i; j--)
{
score[j] = score[j - 1];
}
score[i] = iscore;
break;
}
}
printSort(score,11); //输出十一名学生排序后的成绩
int k;
//逆序
for(i = 0; i < 11 / 2; i++)
{
k = score[i];
score[i] = score[10 - i];
score[10 - i] = k;
}
printSort(score,11); //输出十一名学生逆序后的成绩
return 0;
}
void printSort(int* score,int len)
{
int* iTmp = score;
for (int i = 0; i < len; i++,iTmp++) //输出学生成绩
{
cout << *iTmp <<" ";
}
cout << endl;
}
void bubbleSort(int* score,int len)
{
int i,j,k;
for (i = 0; i < len; i++) //冒泡排序
{
for (j = i; j < len; j++)
{
if(score[i] < score[j])
{
k = score[i];
score[i] = score[j];
score[j] = k;
}
}
}
}
第2个回答 2009-06-15
#include<iostream>
using namespace std;
void xu(double *a,int n);
int main()
{
double a[11];
double b;
cout<<"请输入10名学生的成绩:"<<endl;
for(int i=0;i!=10;++i)
cin>>a[i];
cout<<endl;
cout<<"排序前学生的成绩为:";
for(int i=0;i!=10;++i)
cout<<a[i]<<" ";
xu(a,10);
cout<<endl;
cout<<"排序后学生的成绩为:";
for(int i=0;i!=10;++i)
cout<<a[i]<<" ";
cout<<endl;
cout<<"请再输入一名学生的成绩:";
cin>>a[10];
xu(a,11);
cout<<endl;
cout<<"从高到低学生的成绩为:";
for(int i=0;i!=11;++i)
cout<<a[i]<<" ";
for(int i=0,j=10;i!=j;++i,--j)
{
double t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<endl;
cout<<"从低到高现在学生的成绩为:";
for(int i=0;i!=11;++i)
cout<<a[i]<<" ";
return 0;
}
void xu(double *a,int n)
{
for(int i=0;i!=n;++i)
for(int j=i;j!=n;++j)
{
double max=a[i];
if(a[j]>max)
{
max=a[j];
a[j]=a[i];
}
a[i]=max;
}
}
剩下的部分你自己做吧本回答被提问者采纳