【 HRBUST - 1055】Single(模拟,dp,打表)(总结)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/82945814

题干:

There are many handsome single boys in our team, for example, me. Some times, we like count singles. For example, in the famous “November 11th” (11.11), there are four singles ,so, single is actually 1. For another example, there are 2 singles in the time “1:01”.  We are all boring guys, and here is an boring problem. Time is in the format HH:MM:SS; one day starts from 00:00:00, and ends at 24:00:00, we just want to know how many singles has passed till a certain time (included)

Input

First line is the number of cases: n, the next n lines are each a time, exactly in the format described above. It's guaranteed that the time is in the right range (00:00:00 ~ 24:00:00)

Output

For each case, just give us the answer in a single line, no extra character is needed.

Sample Input


00:00:00 
00:00:01 
00:00:02

Sample Output



1

题目大意:

   给你一个时间t,让你计算从00:00:00到当前时间(包括当前时间这一秒)一共出现了多少次数字1.

解题报告:

   预处理到一个dp数组中就好了。tmp那里也可以直接写成=,然后dp[cur]=dp[cur-1]+tmp。也可以,其实是等价的。

AC代码:

#include<bits/stdc++.h>
#define ll long long

using namespace std;
int dp[24*60*60 + 60*60 + 60 + 5];
int cal(int x) {
	int res=0;
	while(x) {
		if(x%10==1) res++;
		x/=10;
	}
	return res;
}
void init() {
	int tmp=0;
	int a=0,b=0,c=0;//时,分,秒 
	while(a<24) {
		//计算当前状态 
		tmp+=cal(a)+cal(b)+cal(c);
		dp[a*60*60+b*60+c]=tmp;
		//预备下一个状态 
		c++;
		if(c==60) {
			c=0,b++;
			if(b==60) b=0,a++;
		}
	}
}
int main() 
{
	init();
	int t;
	cin>>t;
	while(t--) {
		int a,b,c;
		scanf("%d:%d:%d",&a,&b,&c);
		int tmp=a*60*60+b*60+c;
		printf("%d\n",dp[tmp]);
	}
}

总结:

   熟悉一下模拟时的语句模式和套路:while循环中干两件事情,1.计算当前状态下的值,2.将下一个状态准备好。

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/82945814