递归与排序练习(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;
}
/*使用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;
}
相关文章推荐
- [C How To Program] 习题6.32 递归的选择排序
- c语言递归练习习题
- 母函数练习(HDOJ习题)--QQ日志
- 一月十日练习习题,1数组数据存入字典并输出2降序 和倒叙 输出数组中内容3对字符串当中信息进行查找是否存在4 把数组当中信息按照中文排序
- 《算法导论》习题2.3-4 插入排序的递归版本
- (课本习题)一个简单的用递归解决的小程序
- 一个关于ArrayList的自定义排序小练习
- 实现一个自定义字符串的排序 ,重写Comparable接口的练习!!
- 快速排序递归和非递归算法
- HDOJ 1106 排序 [stable_sort]
- c++ 选择排序 练习
- 选择排序的递归实现
- 求一道“编译原理---清除左递归”习题的解答
- 数据结构练习:排序
- 单链表逆置的递归与非递归算法 [简单的小算法练习]
- 面试题之金山(函数练习2)_字符排序(字母、数字及其它字符)ParseString
- 递归排序的一种实现方法。
- 数据结构学习 之 链表 练习 链表排序功能.(热身练习)
- c++ 选择排序 练习
- 两种归并排序算法的实现:二路归并排序和基本归并排序(虚拟消除递归的二路归并排序)