您的位置:首页 > 其它

poj 2051解题报告

2008-12-09 23:17 330 查看
这是我除了a+b外唯一秒杀的题.当然一次搞定是要付出代价的,看代码就知道了.这道题主要考的就是qsort的应用(又是qsort -_-!)另外波波觉得这道题比较有实际应用.

#include <stdio.h>

#include <memory.h>

#include <stdlib.h>

#include <string.h>

//数据储存结构

typedef struct work{

    int Q_number;

    int period;

    int lastTime;

} work;

work w1[1005],w2,w3[1005],*w4;

//qsort的比较函数

int compare(const void *p1,const void *p2)

{

    work a=*(work *)p1;

    work b=*(work *)p2;

    if(a.period > b.period )

        return 1;

    else if(a.period  == b.period  && a.Q_number  > b.Q_number )

        return 1;

    return -1;

}

int main()

{

    int wCount = 0,j,i,k;

    char s[20];

    //初始化

    while(gets(s) && s[0] != '#') {

        sscanf(s,"Register %d%d",&w1[wCount].Q_number,&w1[wCount].period);

        w1[wCount].lastTime = w1[wCount].period;

        wCount++;

    }

    qsort(w1,wCount,sizeof(work),compare);

    memcpy(w3,w1,wCount*sizeof(work));

    scanf("%d",&k);

    for(i = 0; i < k; i++) {

        printf("%d/n",w1[0].Q_number);

        // 对数据进行更新

        w1[0].lastTime += w1[0].period;

        w2 = w1[0];

        //将头元素按大小插入数组中,比较累赘

        for( j = 1; j < wCount; j++)

            if( w1[j].lastTime > w1[0].lastTime || ( w1[j].lastTime == w1[0].lastTime && w1[j].Q_number > w1[0].Q_number))

                break;

        memcpy(w3,w1 + 1, (j - 1) * sizeof(work));

        w3[j-1] = w2;

        memcpy(w1,w3,wCount*sizeof(work));

    }

}

 

这道题相当的浪费空间和时间,不过还是比波波用c++写出来的代码快了100%  ^_^
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: