qsort sort 用法

*************************************START************************************************

<本文中排序都是采取的从小到大排序> 
一、对int类型数组排序



double in[100];

int cmp( const void *a , const void *b ) 

{ 

    return *(double *)a > *(double *)b ? 1 : -1; 

}



qsort(in,100,sizeof(in[0]),cmp);


二、对char类型数组排序(同int类型)



char word[100]; 

//Sample: 

int cmp( const void *a , const void *b ) 

{ 

    return *(char *)a - *(char*)b; 

}

qsort(word,100,sizeof(word[0]),cmp)


三、对double类型数组排序(希罕要重视)



double in[100];

int cmp( const void *a , const void *b ) 

{ 

    return *(double *)a > *(double *)b ? 1 : -1; 

}



qsort(in,100,sizeof(in[0]),cmp);


四、对布局体一级排序



struct In {

double data; 

int other; 

}s[100] 

//遵守data的值从小到大将布局体排序,关于布局体内的排序关键数data的



//类型可以很多种,参考上方的例子写 

int cmp( const void *a ,const void *b) 

{ 

     return (*(In *)a).data > (*(In *)b).data ? 1 : -1; 

} 

qsort(s,100,sizeof(s[0]),cmp);


五、对布局体二级排序



struct In { 

   int x; int y; 

}s[100]; 

//遵守x从小到大排序,当x相等时遵守y从大到小排序 

int cmp( const void *a , const void *b ) 

{ 

    struct In *c = (In *)a; 

    struct In *d = (In *)b; 

    if(c->x != d->x) return c->x - d->x; 

    else return d->y - c->y; 

} 

qsort(s,100,sizeof(s[0]),cmp);


六、对字符串进行排序



struct In { 

   int data; char str[100]; 

}s[100]; 

//遵守布局体中字符串str的字典次序排序 

int cmp ( const void *a , const void *b ) 

{

    return strcmp( (*(In *)a)->str , (*(In *)b)->str ); 

} 

qsort(s,100,sizeof(s[0]),cmp);



int cmp( const void *a , const void *b )

{

return strcmp((char *)a,(char *)b);

}//(字符串数组)



int compare(const void *a,const void *b)

{

return strcmp(*(char **)a,*(char **)b);

}//(字符串指针数组)


七、策画几何中求凸包的cmp



int cmp(const void *a,const void *b) 

//重点cmp函数,把除了1点外的所有点,扭转角度排序 

{ 

    struct point *c=(point *)a; 

    struct point *d=(point *)b; 

    if( calc(*c,*d,p[1]) < 0) return 1; 

    else if( !calc(*c,*d,p[1]) 

   && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) 

   //若是在一条直线上,则把远的放在前面 

   return 1; else return -1; 

}


  c说话中qsort函数包含在<stdlib.h>的头文件里,strcmp包含在<string.h>的头文件里。

重视:可以的话最好还是本身写qsort( )而不是调用stdlib.h里的qsort()函数,那样效力会高很多。

*************************************END*******************************


Problem Description

有一个背包,背包容量是M=150。有7个物品,物品可以分割成任意大小。
要求尽可能让装入背包中的物品总价值最大,但不能超过总容量。
物品  A  B  C  D  E  F  G
重量  35  30  60  50  40  10  25
价值  10  40  30  50  35  40  30
分析:
目标函数: ∑pi最大
约束条件是装入的物品总重量不超过背包容量:∑wi<=M( M=150)

Input

首先输入一个正整数T,表示有T组测试数据,接下来是T组数据,每组两行,分别为7个物品的重量和价值。

Output

每组数据的背包能装的最大总价值,精确到小数点后三位。

Sample Input

1	
35  30  60  50  40  10  25
10  40  30  50  35  40  30

Sample Output

190.625
#include <iostream>
#include <stdio.h>
using namespace std;

float sum, total, temp;
struct ti{
    float w;
    float v; 
    float mean;    
}tim[7];

int cmp(ti a, ti b){
    return a.mean>b.mean;    
}

void solve(){
    sum=0;
    total=150;
    
    for(int i=0; i<7 && total>0;i++){
        //printf("w=%f    v=%f   mean=%f\n", tim[i].w, tim[i].v, tim[i].mean);
        if(total-tim[i].w>0){
            total-=tim[i].w;
            sum+=tim[i].v;    
        }     
        else{
            sum+=total*tim[i].mean;
            break;   
        }
    }    
}

int 
main()
{
    int T;
    cin>>T; 
    while(T--){
        for(int i=0; i<7; i++){
            scanf("%f", &tim[i]);
        }
        for(int i=0; i<7; i++){
            scanf("%f", &tim[i].v);
            tim[i].mean=tim[i].v/tim[i].w;   
        }
        
        sort(tim, tim+7, cmp);
        solve();
        printf("%.3f\n", sum);
    }    
    
    return 0;
}

你可能感兴趣的:(qsort sort 用法)