程序存储问题(最优存储)

程序存储问题(最优存储)_第1张图片

 程序存储问题(最优存储)_第2张图片

具体代码:

#include 
#include 
#include 
using namespace std;
bool cmp(float x, float y) {
    return x > y;
}
int main()
{
    ifstream inputfile("input.txt");
    ofstream outputfile("output.txt");
    int n;            //文件数目
    int t=0;            //期望时间
    inputfile >> n;
    float a[n+1];
    float b[n+1];
    float sum=0;
    for(int i=1;i<=n;i++)
    {
        inputfile >> a[i];
        sum+=a[i];
    }
    sort(a+1, a+n+1, cmp);  //升序排列
    int k=(n+1)/2;
    for(int i=1;i<(n+1)/2;i++)
    {
        b[i]=a[n-2*i+1];
    }
    int j=0;
    for(int i=n;i>(n+1)/2;i--)
    {
        b[i]=a[n-2*j];
        j++;
    }
    b[k]=a[1];
    for(int i=1;i

算法复杂度;数组单元耗时O(1),sort()耗时O(nlogn);计算检索时间的双重循环耗时O(n^{2}),因此算法耗时O(n^{2})。

 

 

你可能感兴趣的:(c++,算法)