首页 > ACM题库 > HDU-杭电 > hdu 2395 Alarm Clock[解题报告]C++
2014
01-05

hdu 2395 Alarm Clock[解题报告]C++

Alarm Clock

问题描述 :

Advanced courses in computer science often include many interesting optimization problems. The following optimization problem is not advanced, nor is it particularly interesting. You are to write a program to determine the minimum number of buttons a person must push to set their alarm clock. Assume the alarm clock has 7 buttons � hour-up, hour-down, tens-minute-up, and tens-minute-down, ones-minute-up, ones-minute-down, and am/pm. For example, continuously pushing the hour-up button will cause hour digit to go through the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, …. Pushing the hour-down button will cause the sequence to go in the reverse order. Pushing the tens-minute-up buttons cycles tens minute digit through the numbers 0, 1, 2, 3, 4, 5, 0, 1, … Pushing the ones-minute-up button cycles the one digit through the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, … Pushing the am/pm button causes toggles the am/pm indicator.

输入:

The first line of input will contain an integer indicating the number of problems that need to be processed. Each line will contain two times � the first one the current time and the second one the desired time. All times will have the format: 1 or 2-digit hour; followed by a colon; followed by a 2-digit minute; followed by “am” or “pm”. A single space will separate the two times.

输出:

The first line of input will contain an integer indicating the number of problems that need to be processed. Each line will contain two times � the first one the current time and the second one the desired time. All times will have the format: 1 or 2-digit hour; followed by a colon; followed by a 2-digit minute; followed by “am” or “pm”. A single space will separate the two times.

样例输入:

6
7:30am 7:30am
7:30am 7:33am
7:30am 7:20am
7:30am 7:27am
7:30am 7:30pm
7:10am 7:50am

样例输出:

Going from 7:30am to 7:30am requires 0 pushes.
Going from 7:30am to 7:33am requires 3 pushes.
Going from 7:30am to 7:20am requires 1 push.
Going from 7:30am to 7:27am requires 4 pushes.
Going from 7:30am to 7:30pm requires 1 push.
Going from 7:10am to 7:50am requires 2 pushes.

/*
hdu 2395 Alarm Clock
这题太水了
上下午 与 小时不关联
小时 与 分的十位不关联
分的十位 与 个位不关联
*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int cha(int a,int b,int mod)
{
	int ret=1000;
	if(b-a>0&&b-a<ret)
	{
			ret=b-a;
	}
	if(a-b>0&&a-b<ret)
	{
			ret=a-b;
	}
	if(a+mod-b>0&&a+mod-b<ret)
	{
		ret=a+mod-b;
	}
	if(b+mod-a>0&&b+mod-a<ret)
	{
		ret=b+mod-a;//仅为了之前的 键盘误 ret=a+mod-b;
	}
	return ret;
}
int main()
{
	int t,ret;
	int h1,h2,m1,m2,n1,n2;
	char p1,p2;
	string s1,s2;
	cin>>t;
	while(t--)
	{
		ret=0;
		cin>>s1>>s2;
		stringstream strs;
		strs.str(s1);
		strs>>h1;
		strs.get();
		m1=strs.get()-'0';
		n1=strs.get()-'0';
		p1=strs.get();

		strs.str(s2);

		strs>>h2;
		strs.get();
		m2=strs.get()-'0';
		n2=strs.get()-'0';
		p2=strs.get();

		if(p1!=p2)
			++ret;
		if(h1!=h2)
		{
			ret+=cha(h1,h2,12);
		}
		if(m1!=m2)
		{
			ret+=cha(m1,m2,6);
		}
		if(n1!=n2)
		{
			ret+=cha(n1,n2,10);
		}
		cout<<"Going from "<<s1<<" to "<<s2<<" requires "<<ret<<((ret==1)?" push.":" pushes.")<<endl;
	}
	return 0;
}

解题转自:http://blog.csdn.net/qq172108805/article/details/8777619


  1. [email protected]

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  4. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。