zoukankan      html  css  js  c++  java
  • flattern

    #include <stdio.h>
    #include <stdlib.h>
    
    int test_main(int data[100], int dumpCount);
    /*
    int test_main(int data[100], int dumpCount)
    {
        int map[101]={0};
        for(int i=0;i<100;i++){
            map[data[i]]++;
        }
        int l=100;
        int h=0;
        for(int i=0;i<101;i++){
            if(map[i]!=0){l=i;break;}
        }
        for(int i=100;i>=0;i--){
            if(map[i]!=0){h=i;break;}
        }
        while(dumpCount>0){
            while(map[h]>0&&map[l]>0&&dumpCount>0){
                map[h]--;
                map[l]--;
                map[h-1]++;
                map[l+1]++;
                dumpCount--;
            }
            if(map[h]==0)h=h-1;
            if(map[l]==0)l=l+1;
            if(h==l||h-l==1)break;
        }
        int answer=h-l;
        return answer;
    }
    */
    #define MAX 100
    int test_main(int data[MAX], int dumpCount)
    {
        /*for(int i=0;i<MAX;i++){
            for(int j=i+1;j<MAX;j++){
                if(data[i]>data[j]){
                    int temp=data[i];
                    data[i]=data[j];
                    data[j]=temp;
                }
            }
        }
        */
    
        int temp[101]={0};
        for(int i=0;i<MAX;i++){
            temp[data[i]]++;
        }
        int l=100;
        int h=0;
        for(int i=0;i<100+1;i++){
            if(temp[i]!=0) h=i;
        }
        for(int j=100;j>=0;j--){
            if(temp[j]!=0) l=j;
        }
        while(dumpCount>0){
            while(temp[h]!=0&&temp[l]!=0&&dumpCount>0){
                temp[h]--;
                temp[l]--;
                temp[h-1]++;
                temp[l+1]++;
                dumpCount--;
            }
            if(temp[h]==0)h=h-1;
            if(temp[l]==0)l=l+1;
            if(h==l||h-l==1)
                break;
        }
        int Answer=h-l;
        return Answer;
    }
    
    
    
    void build_data(int data[100], int* dumpCount)
    {
        for (int i = 0; i < 100; i ++)
        {
            data[i] = rand() % 100 + 1; 
        }
        *dumpCount = rand() % 1000 + 1;
    }
    
    void main(void)
    {
        int data[100];
        int dumpCount;
    
        for (int l = 0; l < 10; l++)
        {
            build_data(data, &dumpCount);
            printf("%d
    ", test_main(data, dumpCount));
        }
    }
  • 相关阅读:
    svn搭建多版本共存记录
    python中使用redis
    小程序之使用腾讯地图获取经纬度
    vue路由元之进入路由需要用户登录权限功能
    input type="tel" 数字输入框显示圆点
    input在IOS中的聚焦问题
    JS实现手机号码中间4位变星号
    CSS实现div填充剩余高度
    小程序之地图导航
    小程序之点击图片放大预览
  • 原文地址:https://www.cnblogs.com/ZzznOoooo/p/6628067.html
Copyright © 2011-2022 走看看