#include
#define nmax 1000010
using namespace std;
void bubble_sort(int* p, int len)//函数实现
{
int i = 0;
int j = 0;
for (i = 0; i < len - 1; i++)//需要进行len-1趟
{
int flag = 1;
for (j = 0; j < len - 1 - i;j++)//每趟两两比较未排好序元素个数len-1次。
{
if (p[j] > p[j+1])
{
int tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
flag = 0;
}
}
if (flag==1)//判断是否排好序
break;
}
}
int main( )
{
int n,p,t[nmax];
int i,tmin=0;
cin>>n>>p;
for(i=0;i
cin>>t[i];
bubble_sort(t,n);
for(i=0;i
tmin+=t[i];
cout<
return 0;
}
用冒泡排序法对输入的数组t[n]排序,求出前p项之和即为tmin。
样例1:
输入
5 3
1 5 4 3 2
输出
3
t[nmax]太大了
根据输入申请内存
int *t;
cin >> n >> p;
t = new int[n];