题目大意
输入现在的时间,以及以二进制表示的经过的秒数,计算经过这么多秒后的准确时间。
注意到这是一道模拟题,但是我们要把二进制转换为十进制。因为长度较短,所以考虑暴力从高位到低位枚举并加和:
int time=0;
string st;
cin>>st;
for(int i=0;i<st.length();i++){
time+=st[i]==0?0:pow(1,st.length()-i-1);
}接下来就简单了,首先对秒做带余除法,商加在分上,对分做带余除法,商加在时上,以此类推,月份的天数存在数组里然后一次次减。(长难句警告)
m+=(s+time)/60;
s=(s+time)%60;
h+=m/60;
m%=60;
day+=h/24;
h%=24;
while(day-mon[month]+((year%4==0&&(year%100!=0||year%400==0)&&month==2)?1:0)>0){
day-=mon[month]+((year%4==0&&(year%100!=0||year%400==0)&&month==2)?1:0);
month++;
if(month>12){
year++;
month=1;
}
}AC Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
signed main(){
int year,month,day,h,m,s;
cin>>year;
while(year!=0){
scanf("/%lld/%lld %lld:%lld:%lld",&month,&day,&h,&m,&s);
int time=0;
string st;
cin>>st;
for(int i=0;i<st.length();i++){
time+=st[i]==0?0:pow(2,st.length()-i-1);
}
m+=(s+time)/60;
s=(s+time)%60;
h+=m/60;
m%=60;
day+=h/24;
h%=24;
//printf("%lld/%s%lld/%s%lld %s%lld:%s%lld:%s%lld\n",year,month>=10?"":"0",month,day>=10?"":"0",day,h>=10?"":"0",h,m>=10?"":"0",m,s>=10?"":"0",s);
while(day-mon[month]+((year%4==0&&(year%100!=0||year%400==0)&&month==2)?1:0)>0){
day-=mon[month]+((year%4==0&&(year%100!=0||year%400==0)&&month==2)?1:0);
month++;
//printf("%lld/%s%lld/%s%lld %s%lld:%s%lld:%s%lld\n",year,month>=10?"":"0",month,day>=10?"":"0",day,h>=10?"":"0",h,m>=10?"":"0",m,s>=10?"":"0",s);
if(month>12){
year++;
month=1;
}
}
printf("%lld/%s%lld/%s%lld %s%lld:%s%lld:%s%lld\n",year,month>=10?"":"0",month,day>=10?"":"0",day,h>=10?"":"0",h,m>=10?"":"0",m,s>=10?"":"0",s);
cin>>year;
}
}