【 HRBUST - 1055】Single(模拟,dp,打表)(总结)
題干:
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
3?
00:00:00?
00:00:01?
00:00:02
Sample Output
0?
1?
1
題目大意:
? ?給你一個時間t,讓你計算從00:00:00到當前時間(包括當前時間這一秒)一共出現了多少次數字1.
解題報告:
? ?預處理到一個dp數組中就好了。tmp那里也可以直接寫成=,然后dp[cur]=dp[cur-1]+tmp。也可以,其實是等價的。
AC代碼:
#include<bits/stdc++.h> #define ll long longusing 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.將下一個狀態準備好。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【 HRBUST - 1055】Single(模拟,dp,打表)(总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡额度可以全部刷完吗 不知道的人要吃
- 下一篇: 【HDU - 3068】最长回文(Man