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

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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值