HDU 1209 Clock(模拟)
2015-11-22 15:34
274 查看
题目地址:点击打开链接
题意:先按时针和分针的角度排序,如何角度相等则按时间排序
思路:刚开始是想着时针和分针对0点的角度加起来,考虑的情况多且比较复杂,后来角度直接减取绝对值就可
m点n分k秒时
时针从0点转过的角度a = [m + n/60 + k/3600]*30
= 30m + n/2 + k/120(度)
分针转过的角度b = [n/60 + k/3600]*360
= 6n + k/10(度)
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
struct node
{
int h,s;
double r;
}a[6];
bool cmp(struct node a,struct node b)
{
if(a.r != b.r)
return a.r < b.r;
else
return a.h < b.h;
}
int main()
{
int t;
int i;
scanf("%d",&t);
while(t--)
{
for(i=0; i<5; i++)
{
scanf("%d:%d",&a[i].h,&a[i].s);
if(a[i].h >= 12)
{
a[i].r = abs(30.0 * (a[i].h-12) + a[i].s / 2.0 - a[i].s * 6.0);
}
else
{
a[i].r = abs(30.0 * a[i].h + a[i].s / 2.0 - a[i].s * 6.0);
}
if(a[i].r > 180.0)
a[i].r = 360.0 - a[i].r;
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].h,a[2].s);
}
return 0;
}
错误代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
struct node
{
int h,s;
double r;
}a[6];
bool cmp(struct node a,struct node b)
{
if(a.r != b.r)
return a.r < b.r;
else
return a.h < b.h;
}
int main()
{
int t;
int i;
scanf("%d",&t);
while(t--)
{
for(i=0; i<5; i++)
{
scanf("%d:%d",&a[i].h,&a[i].s);
if(a[i].h >= 12)
{
a[i].r = 30.0 * (a[i].h-12) + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
}
else
{
a[i].r = 30.0 * a[i].h + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
}
if(a[i].r > 180.0)
a[i].r = 360.0 - a[i].r;
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].h,a[2].s);
}
return 0;
}
题意:先按时针和分针的角度排序,如何角度相等则按时间排序
思路:刚开始是想着时针和分针对0点的角度加起来,考虑的情况多且比较复杂,后来角度直接减取绝对值就可
m点n分k秒时
时针从0点转过的角度a = [m + n/60 + k/3600]*30
= 30m + n/2 + k/120(度)
分针转过的角度b = [n/60 + k/3600]*360
= 6n + k/10(度)
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
struct node
{
int h,s;
double r;
}a[6];
bool cmp(struct node a,struct node b)
{
if(a.r != b.r)
return a.r < b.r;
else
return a.h < b.h;
}
int main()
{
int t;
int i;
scanf("%d",&t);
while(t--)
{
for(i=0; i<5; i++)
{
scanf("%d:%d",&a[i].h,&a[i].s);
if(a[i].h >= 12)
{
a[i].r = abs(30.0 * (a[i].h-12) + a[i].s / 2.0 - a[i].s * 6.0);
}
else
{
a[i].r = abs(30.0 * a[i].h + a[i].s / 2.0 - a[i].s * 6.0);
}
if(a[i].r > 180.0)
a[i].r = 360.0 - a[i].r;
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].h,a[2].s);
}
return 0;
}
错误代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
struct node
{
int h,s;
double r;
}a[6];
bool cmp(struct node a,struct node b)
{
if(a.r != b.r)
return a.r < b.r;
else
return a.h < b.h;
}
int main()
{
int t;
int i;
scanf("%d",&t);
while(t--)
{
for(i=0; i<5; i++)
{
scanf("%d:%d",&a[i].h,&a[i].s);
if(a[i].h >= 12)
{
a[i].r = 30.0 * (a[i].h-12) + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
}
else
{
a[i].r = 30.0 * a[i].h + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
}
if(a[i].r > 180.0)
a[i].r = 360.0 - a[i].r;
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].h,a[2].s);
}
return 0;
}
相关文章推荐
- 查询优化(一:Bookmark Lookup)
- php的延迟静态绑定
- 在centos7搭建上svn,并用svn和http访问
- 再谈沟通
- 在jsp中连接数据库的用法(增删改查)
- 工厂设计模式
- DFS & BFS
- Hibernate基础
- 第八章 异常控制流
- 源代码管理和git 使用
- YII 路径总结
- javascript冒泡排序
- ios 方面常见的一些面试问题
- 在C++中使用golang的协程
- ThinkPHP学习-1
- ZZULIOJ 1787 生化危机 (vector+DFS)
- UIStactView 新特性语法新介绍
- LeetCode OJ——Generate Parentheses
- 第八章 异常控制流
- 地图定位