您的位置:首页 > 其它

递归与排序练习(HDOJ习题)--QQ迁移

2014-06-24 21:58 337 查看
1029.c(是递归题)
/*使用time[i].x=abs((time[i].h)%12*60+time[i].m-12*time[i].m);则为15MS 176K

使用time[i].x=(time[i].h)%12*60+time[i].m-12*time[i].m;

if(time[i].x<0){

time[i].x*=-1;

}

则为0MS 176K

*/

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node{

int h;

int m;

int x;

}time[10];

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

struct node *c = (struct node *)a;

struct node *d = (struct node *)b;

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

return c->x - d->x;

else

return c->h - d->h;

}

int main(){

int t,i;

scanf("%d",&t);

while(t--){

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

scanf("%2d:%2d",&time[i].h,&time[i].m);

//time[i].x=abs((time[i].h)%12*60+time[i].m-12*time[i].m);

time[i].x=(time[i].h)%12*60+time[i].m-12*time[i].m;

if(time[i].x<0){

time[i].x*=-1;

}

if(time[i].x>=360){

time[i].x=720-time[i].x; //time[i].x-=360;则WA

}

}

qsort(time,5,sizeof(time[0]),cmp);

printf("%02d:%02d\n",time[2].h,time[2].m);//占位符的使用

}

return 0;

}
zhbb
1209.double.c
#include <stdio.h>

#include <stdlib.h>

#define EXP 1e-8

typedef struct

{

int hh;

int mm;

double v;

}time;

double myabs(double x)

{

return x>0 ? x : -x;

}

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

{

time c = *(time *)a;

time d = *(time *)b;

if (myabs(c.v-d.v)<EXP)

return c.hh - d.hh;

else

return c.v > d.v ? 1 : -1;

}
double gao(int hh, int mm)

{

double h, m, t;

if (hh>=12) hh -= 12;

h = (hh*60+mm)*1.0 / 720;

m = mm * 1.0 / 60;

t = myabs(h-m);

if (t>=0.5)

t = 1 - t;

return t;

}

int main()

{

time a[5];

int t, i;

scanf("%d", &t);

while (t--)

{

for (i=0; i<5; i++)

{

scanf("%d:%d", &a[i].hh, &a[i].mm);

a[i].v = gao(a[i].hh, a[i].mm);

}

qsort(a, 5, sizeof(a[0]), cmp);

printf("%02d:%02d\n", a[2].hh,a[2].mm);

}

return 0;

}
1209.int.c
#include <stdio.h>

#include <stdlib.h>

#define EXP 1e-8

typedef struct

{

int hh;

int mm;

int v;

}time;

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

{

time c = *(time *)a;

time d = *(time *)b;

if (c.v == d.v)

return c.hh - d.hh;

else

return c.v - d.v;

}
int gao(int hh, int mm)

{

int h, m, t;

if (hh>=12) hh -= 12;

h = hh * 60 + mm;

m = mm * 12;

t = h-m;

if (t<0) t*=-1;

if (t>=360)

t = 720 - t;

return t;

}

int main()

{

time a[5];

int t, i;

scanf("%d", &t);

while (t--)

{

for (i=0; i<5; i++)

{

scanf("%d:%d", &a[i].hh, &a[i].mm);

a[i].v = gao(a[i].hh, a[i].mm);

}

qsort(a, 5, sizeof(a[0]), cmp);

printf("%02d:%02d\n", a[2].hh,a[2].mm);

}

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: