3144. 问候

Fifnmar
#include <iostream>
#include <string>
#include <cstdint>
#include <cmath>
using namespace std;
using u32 = uint32_t;

int main() {
    u32 t; cin >> t;
    for (u32 i = 0; i < t; ++i) {
        cout << "case #" << i << ":\n";
        u32 n, h = 0, m = 0; cin >> n;
        double s = 0;
        for (u32 j = 0; j < n; ++j) {
            string time; cin >> time;
            u32 hh, mm;
            double ss;
            sscanf(time.data(), "%u:%u:%lf", &hh, &mm, &ss);
            h += hh; // n is small enough, and u32 & double can hold all data
            m += mm; // sumed up, to postpone carries. So we do not carry
            s += ss; // eagerly. ^_^
        }

        /* Process carrying */ {
            m += s / 60;
            s = remainder(s, 60);
            auto [quot, rem] = div(m, 60);
            h += quot;
            m = rem;
            h %= 24;
        }

        if (h >= 4 && h < 12) {
            cout << "Good morning!\n";
        } else if (h >= 12 && h < 18) {
            cout << "Good afternoon!\n";
        } else if (h >= 18 && h < 22) {
            cout << "Good evening!\n";
        } else {
            cout << "Good night!\n";
        }
    }
}
aiden
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
struct Time
{
    int hour;
    int min;
    double sec;
};
enum period
{
    morning, afternoon, evening, night
};
period getAns(vector<Time> &vec)
{
    Time init = vec[0];
    Time sum({ 0,0,0 });
    for (const auto &i : vec)
    {
        sum.hour += i.hour;
        sum.min += i.min;
        sum.sec += i.sec;
    }
    while (sum.sec >= 60)
    {
        sum.sec -= 60;
        ++sum.min;
    }
    while (sum.min >= 60)
    {
        sum.min -= 60;
        ++sum.hour;
    }
    while (sum.hour >= 24)
    {
        sum.hour -= 24;
    }
    if (sum.hour >= 4 && sum.hour < 12)
        return morning;
    if (sum.hour >= 12 && sum.hour < 18)
        return afternoon;
    if (sum.hour >= 18 && sum.hour < 22)
        return evening;
    if (sum.hour >= 22 || sum.hour < 4)
        return night;
}
int main()
{
    int t;
    cin >> t;
    for (auto z = 0; z < t; ++z)
    {
        vector<Time> vec;
        int n;
        cin >> n;
        while (n--)
        {
            Time buff;
            scanf("%d:%d:%lf", &buff.hour, &buff.min, &buff.sec);
            vec.push_back(buff);
        }
        period ans = getAns(vec);
        cout << "case #" << z << ":" << endl;
        if (ans == morning)
            cout << "Good morning!" << endl;
        if (ans == afternoon)
            cout << "Good afternoon!" << endl;
        if (ans == evening)
            cout << "Good evening!" << endl;
        if (ans == night)
            cout << "Good night!" << endl;
    }
    return 0;
}
xcc120

直接用60进制转换的话会比较烦。。需要fmod函数

include

include

int main()
{
int hour,minute,n,i,k,m,j;
double second,N,s;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
scanf(“%d”,&k);
N=0,s=0,m=0;
printf(“case #%d:\n”,i-1);
for(j=1;j<=k;j++)
{
scanf(“%d:%d:%lf”,&hour,&minute,&second);

        s+=second,m+=s/60.0,s=fmod(s,60.0);
        m+=minute;
        N+=m/60, m=m%60;
        N+=hour;
    }
    N=fmod(N,24.0);
    if(N<=3||N>=22)
        printf("Good night!\n");
    else if(N>=4&&N<=11)
            printf("Good morning!\n");
            else if(N>=12&&N<=17)
                    printf("Good afternoon!\n");
                else
                    printf("Good evening!\n");

}

return 0;

}

Li Dao

全部转为秒数,用double来算

include

using namespace std;
int T;
double now;
double cal_time(const string& tmp)
{
double hour,minute,seconds;
sscanf(tmp.c_str(),”%lf:%lf:%lf”,&hour,&minute,&seconds);
return hour3600+minute60+seconds;
}
void solve()
{
int n;
cin>>n;
now=0;
for(int i=1;i<=n;i++)
{
string tmp;
cin>>tmp;
now+=cal_time(tmp);
}
while(now>=86400) now-=86400;
if(now-14000>=0 && now-43200<0) cout<<”Good morning!”<=0 && now-64800<0) cout<<”Good afternoon!”<=0 && now-79200<0) cout<<”Good evening!”<<endl;
else cout<<”Good night!”<<endl;
return;
}

int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

你当前正在回复 博客/题目
存在问题!