NVIDIA笔试2018-09-04思路
统计日志记录最少天数
题目:
Input
The first input line contains number n (1 ≤ n ≤ 100). The following n lines contain recordings in format «[time]: message», where time is given in format «hh:mm x.m.». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character «a» or character «p». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20.
Output
Output one number — the minimum amount of days covered by the log.
Example:
input
5
[05:00 a.m.]: Server is started
[05:00 a.m.]: Rescan initialized
[01:13 p.m.]: Request processed
[01:10 p.m.]: Request processed
[11:40 p.m.]: Rescan completed
output
2
Example:
input
3
[09:00 a.m.]: User logged in
[08:00 a.m.]: User logged in
[07:00 a.m.]: User logged in
output
3
解析
给你n行字符串是一个公司服务器的登录记录,每一行都有时间记录。输出可能的最小天数。
题解:
一:就是重复的时间算一天的,但是超过10条就是第二天的呢,
二:最坑的就是不符合日常生活逻辑,am 12点算的是凌晨。其余和和日常一样。
相同时间是一个一个加,取末尾数字,判断是否是0,也就是相同记录大于10个的算作另一天。
代码(C++)
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[50];
int x[110],time[110];
int n,ans,a,b,cnt;
while(cin>>n)
{
ans=1;
memset(time,0,sizeof(time));
memset(x,0,sizeof(x));
getchar();
for(int i=1; i<=n; i++)
{
gets(s);
a=(s[1]-'0')*10+(s[2]-'0');
b=(s[4]-'0')*10+(s[5]-'0');
if(a==12&&s[7]=='a')///am 12点算作凌晨
a=0;
else if(a!=12&&s[7]=='p')
a+=12;
x[i]=a*60+b; ///把每天时间转成分钟
if(x[i]==x[i-1])
{
time[i]=time[i-1]+1;
time[i]%=10;
}
// if(x[i]==x[i-1])
// cnt++; ///记录重复条数,后面/10就是看看超过10条的记录
// ///加在总天数上面
}
for(int i=1; i<=n; i++)
{
if(x[i-1]>x[i])
ans++;
else if(x[i]==x[i-1])
{
if(!time[i])
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}