测试输入包含若干测试用例,每个测试用例为一整天的租船纪录,格式为: 船号(1~100) 键值(S或E) 发生时间(小时:分钟) 每一天的纪录保证按时间递增的顺序给出。当读到船号为-1时,全部输入结束,相应的结果不要输出。
对每个测试用例输出1行,即当天的游客租船次数和平均租船时间(以分钟为单位的精确到个位的整数时间)。
1 S 08:10 2 S 08:35 1 E 10:00 2 E 13:16 0 S 17:00 0 S 17:00 3 E 08:10 1 S 08:20 2 S 09:00 1 E 09:20 0 E 17:00 -1
2 196 0 0 1 60
#include <iostream> #include <string> #include <cmath> using namespace std; #define N 101 typedef struct _Boat { int startH, startM; int endH, endM; }Boat; int rent_time(const Boat &bt) { return (bt.endH * 60 + bt.endM) - (bt.startH * 60 + bt.startM); } int main() { Boat bts[N]; int count = 0, total_time = 0; for (int i = 0; i < N; i++) bts[i].startH = bts[i].startM = bts[i].endH = bts[i].endM = -1; int No = 0; char SE; string str_time; while (cin>>No && -1 != No) { cin >> SE >> str_time; if (0 == No) { double avg = count > 0 ? (double)total_time / (double)count : 0; cout << count << " " << round(avg) << endl; count = 0; total_time = 0; for (int i = 0; i < N; i++) bts[i].startH = bts[i].startM = bts[i].endH = bts[i].endM = -1; } else { if ('S' == SE) { bts[No].startH = (str_time[0] - '0') * 10 + (str_time[1] - '0'); bts[No].startM = (str_time[3] - '0') * 10 + (str_time[4] - '0'); } else if ('E' == SE) { bts[No].endH = (str_time[0] - '0') * 10 + (str_time[1] - '0'); bts[No].endM = (str_time[3] - '0') * 10 + (str_time[4] - '0'); if (-1 != bts[No].startH) { ++count; total_time += rent_time(bts[No]); //bts[No].startH = -1; } } } } return 0; }
while True: try: boatRecord = {} #使用字典保存对应船号的借出时间 rentNum = 0 #租船次数 rentTime = 0 #租船总时间 tempInput = input().split() if tempInput[0] == '-1': #输入为-1结束程序 break while tempInput[0] != '0': #直到输入为0结束此次测试用例 if tempInput[1] == 'S': #借船,则更新借出时间 boatRecord[tempInput[0]] = list(map(int,tempInput[2].split(":"))) else: #还船 if tempInput[0] in boatRecord: #如果之前有记录借船则计算 rentNum += 1 rentTime += 60 * (int(tempInput[2].split(':')[0]) - boatRecord[tempInput[0]][0]) + int(tempInput[2].split(':')[1]) - boatRecord[tempInput[0][1] tempInput = input().split() if rendNum != 0: #分母不能为0 print('%d %d' % (rentNum,int(rentTime/rentNum+0.5))) else: print('0 0') except Exception: break
此题很***,不想说什么。还船的时候难道不用看先前的状态是否是S??
package com.speical.first;
import java.util.Arrays;
import java.util.Scanner;
/**
* 游船出租
*
* ***题目,不解释
*
* 哈希思想,但是测试用例真的很***啊
* @author special
* @date 2018年1月6日 下午4:12:37
*/
public class Pro126 {
static Node[] map = new Node[100];
static int times, count;
static class Node{
char status;
int[] time = new int[2];
public Node(char status, String time){
this.status = status;
this.time = parseTime(time);
}
public int add(Node node){
return (node.time[0] - time[0]) * 60 + (node.time[1] - time[1]);
}
}
public static int[] parseTime(String times){
int[] time = new int[2];
time[0] = Integer.valueOf(times.substring(0,2));
time[1] = Integer.valueOf(times.substring(3,5));
return time;
}
public static void insert(Node node, int n){
if(node.status == 'S'){ //如果当前是S直接更新当前船的状态
map[n] = node;
}else{
if(map[n] != null){ //若果当前船是E,且不为空,就计算时间。我第二次都没开始用船,直接还船也可以?
count++; //且还船后,还不更新船的状态??
times += map[n].add(node);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n = input.nextInt();
if(n == -1) break;
char status = input.next().charAt(0);
String time = input.next();
if(n == 0){
System.out.println(count + " " + (count == 0 ? 0 : (int)(times / (count * 1.0) + 0.5)));
Arrays.fill(map, null);
times = 0;
count = 0;
}else{
Node node = new Node(status, time);
insert(node, n - 1);
}
}
}
}
/*需要记录的数据:总共出租次数,每艘船租出的时间 Boat中设置一个flag,为false表示未租出,输入数据时与对应船只信息进行比较 */ #include<cstdio> #include<vector> #include<cstring> using namespace std; int TimeCal(char str[]); struct Boat { bool flag = false; int start = 0; //开始租借的时间 int duration = 0; //该船总共借出的时间 }; int main() { Boat boat[101]; int idx; int count = 0; //记录借出的次数 while (scanf("%d", &idx) != EOF && idx != -1) { char status[2], time[6]; scanf("%s %s", status, time); //分成三个if的判断 ///////////如果idx为0,一轮输入结束,统计时间 if (idx == 0) { int ans = 0; double sum = 0; if (count) { for (int i = 1; i <= 100; i++) { sum += boat[i].duration; } ans = (int)(sum / count + 0.5); //这个用来四舍五入的技巧很妙!!! } printf("%d %d\n", count, ans); //这之后记得要初始化一遍,因为一轮输入结束了 count = 0; for(int i = 0; i<101; i++) { boat[i].flag = false; boat[i].duration = boat[i].start = 0; } continue; } //如果输入的状态为租出,则判断该船是否未被借出,如果没有,则标记为借出,赋值借出时间 if (strcmp(status, "S") == 0) { boat[idx].flag = true; boat[idx].start = TimeCal(time); continue; } //如果状态为,还船,判断/计算duration if (boat[idx].flag && strcmp(status, "E") == 0) { boat[idx].flag = false; boat[idx].duration += TimeCal(time) - boat[idx].start; count++; continue; } }//while }//main int TimeCal(char str[]) { int hour, min; hour = (str[0] - '0') * 10 + (str[1] - '0'); min = (str[3] - '0') * 10 + (str[4] - '0'); return hour * 60 + min; }
#include<iostream> #include<string> #include<cstdio> #include<cstring> using namespace std; int t[101],book[101]; int main() { int n,h,m; char state; while(true) { memset(t,0,sizeof(t)); memset(book,0,sizeof(book)); int count = 0,totaltime = 0; while(scanf("%d %c %d:%d",&n,&state,&h,&m) != EOF && n) { if(n == -1) break; if(state == 'S' && book[n] == 0) { book[n] = 1; t[n] = h * 60 + m; } else if(state == 'E' && book[n] == 1) { book[n] = 0; totaltime += h * 60 + m - t[n]; t[n] = 0; count++; } } if(n == -1) break; if(count) cout << count << " " << int(totaltime * 1.0 / count + 0.5) << endl; else cout << 0 << " " << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int getTime(string st, string ed){ string s1=st.substr(0, 2), s2=st.substr(3, 2); int sth=stoi(s1), stm=stoi(s2); //分别得到开始的小时、分钟 s1=ed.substr(0, 2), s2=ed.substr(3, 2); int edh=stoi(s1), edm=stoi(s2); //分别得到结束的小时、分钟 return (edh*60+edm)-(sth*60+stm); //返回时间差 } int main(){ int i, cnt=0, cnttime=0; string time, str, key; map<string, string> mp; while(cin >> key){ if(key == "-1") break; cin >> str >> time; if(key == "0"){ if(cnt == 0) printf("%d %d\n", cnt, cnttime); else printf("%d %.0lf\n", cnt, 1.0*cnttime/cnt); cnt = cnttime = 0; mp.clear(); //清空 continue; } if(str == "S") mp[key] = time; //这里注意 只要出现 'S' 那么就覆盖 if(str == "E" && mp[key] != ""){ //还船 且信息完整 cnt++; cnttime += getTime(mp[key], time); } } return 0; }主要思想是:用map存储开始的时间,键是船号,值是时间(用字符串存储)。只要还船时,对应船号对应的时间不为“”,则计算时间差。
#include <bits/stdc++.h> using namespace std; int time2int(string time) { int h,m; sscanf(time.c_str(),"%d:%d",&h,&m); return h*60+m; } int main() { int no; vector<pair<int,pair<int,int> > > boats(101,pair<int,pair<int,int> >(0,pair<int,int>(0,-1)) ); while(cin>>no&&no!=-1) { char choose; string time; cin>>choose>>time; if(no==0) { int sum=0,cnt=0; for(auto p:boats)if(p.first)sum+=p.second.first,cnt+=p.first; cout<<cnt<<" "<<(sum==0?sum:int(1.0*sum/cnt+0.5))<<endl; boats=vector<pair<int,pair<int,int> > >(101,pair<int,pair<int,int> >(0,pair<int,int>(0,-1)) ); continue; } if(choose=='S')boats[no].second.second=time2int(time); if(choose=='E'&&boats[no].second.second!=-1)boats[no].second.first+=time2int(time)-boats[no].second.second,++boats[no].first; } return 0; }
正确逻辑的代码//但是不知道如果出现连续两个SS应该算那个,只有先S再E才算一次 #include<iostream> (720)#include<cstring> using namespace std; struct boat{ int time[20]; char flag[20]; int ans = 0; }b[101]; int change(string s){ int idex = s.find(":"); int hour = 0,minute = 0,i; for(i=0;i<idex;i++){ hour = hour*10+s[i]-'0'; } for(i=idex+1;i<s.size();i++){ minute = minute*10+s[i]-'0'; } return hour*60+minute; } int main(){ int num,max_num=0; char c; string s; int i,j; while(cin>>num){ cin>>c>>s; double k = 0,ti = 0; if(num==-1){ break; } else if(num==0){ for(i=1;i<max_num;i++){ if(b[i].ans>0){ for(j=0;j<b[i].ans;j++){ cout<<i<<" "<<b[i].flag[j]<<" "<<b[i].time[j]<<endl; } } } for(i=1;i<=max_num;i++){ for(j=1;j<b[i].ans;){ if(b[i].flag[j]=='E'&&b[i].flag[j-1]=='S'){ k++; ti += b[i].time[j]-b[i].time[j-1]; j += 2; } else if(b[i].flag[j]=='S'&&b[i].flag[j-1]=='E'){ j++; } else if(b[i].flag[j]=='E'&&b[i].flag[j-1]=='E'){ j += 2; } else{ swap(b[i].flag[j],b[i].flag[j-1]); swap(b[i].time[j],b[i].time[j-1]); j++; } } cout<<k<<" "<<ti<<endl; } if(k==0){ cout<<"0 0"<<endl; } else{ cout<<k<<" "<<(int)(ti/k+0.5)<<endl; max_num = 0; } } else{ if(max_num<num){ max_num = num; } int t = change(s); b[num].time[b[num].ans] = t; b[num].flag[b[num].ans++] = c; } } } 牛客网测试通过的代码:只要有一个S,后面所有的E都加一次,用新的S的时间代替旧的S的时间 所以这逻辑有点傻#include<iostream> (720)#include<cstring> using namespace std; struct boat{ int time[20]; char flag[20]; int ans = 0; int start=-1; }b[101]; int change(string s){ int idex = s.find(":"); int hour = 0,minute = 0,i; for(i=0;i<idex;i++){ hour = hour*10+s[i]-'0'; } for(i=idex+1;i<s.size();i++){ minute = minute*10+s[i]-'0'; } return hour*60+minute; } int main(){ int num,max_num=0; char c; string s; int i,j,x; while(cin>>num){ cin>>c>>s; double k = 0,ti = 0; if(num==-1){ break; } else if(num==0){ for(i=1;i<=max_num;i++){ for(j=0;j<b[i].ans;j++){ if(b[i].flag[j]=='S'){ b[i].start = b[i].time[j]; } if(b[i].flag[j]=='E'){ if(b[i].start!=-1){ k++; ti += b[i].time[j]-b[i].start; } } } } if(k==0){ cout<<"0 0"<<endl; } else{ cout<<k<<" "<<(int)(ti/k+0.5)<<endl; max_num = 0; } } else{ if(max_num<num){ max_num = num; } int t = change(s); b[num].time[b[num].ans] = t; b[num].flag[b[num].ans++] = c; } } }
#include <iostream> (720)#include <algorithm> #include <string> (765)#include <vector> using namespace std; struct Record { int no; char tag; string time; friend istream& operator>>(istream& in, Record& r) { cin >> r.no; if(r.no != -1) { cin >> r.tag >> r.time; } return in; } bool operator <(const Record& b)const { return no < b.no; } }; int timetominute(string t) { int minute = ((t.at(0) - '0') * 10 + (t.at(1) - '0')) * 60 + (t.at(3) - '0') * 10 + (t.at(4) - '0'); return minute; } int minustime(string a, string b) { int res; if(a > b) res = timetominute(a) - timetominute(b); else res = timetominute(b) - timetominute(a); return res; } void Handle(vector<Record>& arr) { arr.erase(arr.begin() + arr.size() - 1);//去掉最后那一行收工的记录 stable_sort(arr.begin(), arr.end()); int num = 0; int sum = 0; size_t i = 0; while(i < arr.size()) { if((i + 1) < arr.size() && (arr[i].no == arr[i + 1].no) && arr[i].tag == 'S' && arr[i + 1].tag == 'E' ) { //find a pair //测试数据有问题,只要前面有一个S后面的连续每个E都算一次///////// int k = i; ++i; while(i < arr.size() && arr[i].no == arr[k].no && arr[i].tag == 'E') { ++num; sum += minustime(arr[i].time, arr[k].time); ++i; } /////////////////////////////////////////////////////////////////// //这才是正确的逻辑//////////////////////////////////////////////// // ++num; //sum += minustime(arr[i].time, arr[k].time); //i += 2; ////////////////////////////////////////////////////////////////// } else { i += 1; } } if(num == 0) cout << "0 0\n"; else { float res1 = (float)sum / num; int res = sum / num; if(res1 >= (sum / num + 0.5)) ++res; cout << num << " " << res << endl; } } int main() { Record rec; vector<Record> arr; while(cin >> rec) { if(rec.no == -1) { break; } else { arr.push_back(rec); if(rec.no == 0) { //end of day Handle(arr); arr.clear(); } } } return 0; }
//测试用例有误 #include<iostream> (720)#define N 101 using namespace std; int s[N]; int e[N]; int main(){ int id,h,m; char se; while(scanf("%d ",&id)!=EOF && id!=-1){ scanf("%c %d:%d",&se,&h,&m); if(id!=0){ int time = 60*h+m; if(se=='S') s[id]=time; else e[id]=time; }else{ for(int i=1;i<101;i++){ if(s[i]>0 && e[i]>0){ s[0]=s[0]+e[i]-s[i]; e[0]++; } } if(e[0]==0) printf("%d %d\n",0,0); else{ //四舍五入 int t=s[0]/e[0]; int t_=(double)s[0]/(double)e[0]+0.5; if(t_>t) t++; printf("%d %d\n",e[0],t); } fill(s,s+101,0); fill(e,e+101,0); } } return 0; }
#include <iostream> using namespace std; class Record { public: int no; char RoR; float hour; float minute; }; void totalborrow(Record *boats, int numbers) { if(numbers==0) { cout<<0<<" "<<0<<endl; return; } float validtimes=0,sumtime=0; for(int i = 0;i<numbers-1;i++) { for(int j = i+1;j<numbers;j++) { if((boats[i].RoR=='S'&&boats[j].RoR=='E')&&(boats[i].no==boats[j].no)) { validtimes++; sumtime = sumtime+60*(boats[j].hour-boats[i].hour)+boats[j].minute-boats[i].minute; } } } cout<<validtimes<<" "<<int(sumtime/validtimes+0.5)<<endl; } int main() { int startb; int numbers = 0; Record *boats = new Record[100]; while(cin>>startb&&startb!=-1) { char emoj; int hour,minute; cin>>emoj; scanf("%d:%d",&hour,&minute); if(startb == 0) { totalborrow(boats, numbers); numbers = 0; continue; } else { boats[numbers].no = startb; boats[numbers].RoR = emoj; boats[numbers].hour = hour; boats[numbers].minute = minute; numbers++; } } return 0; }
//有点无语,首先这个还船后起始时间不能更新。而且最后的输出还要四舍五入。 #include<stdio.h> #include<malloc.h> #include<string.h> struct chuan{ int begin; int end; }; chuan all[101]; int get(char *a){ int shi=(a[0]-'0')*10+a[1]-'0'; int fen=(a[3]-'0')*10+a[4]-'0'; return shi*60+fen; } int main(){ int n,k=0,sum=0; for(int i=0;i<101;++i) all[i].begin=all[i].end=-1; while(scanf("%d",&n)!=-1&&n!=-1){ char b[2],buf[100]; scanf("%s%s",b,buf); int w=get(buf); if(n==0){ if(k!=0) printf("%d %d\n",k,(sum+k-1)/k); else printf("0 0\n"); sum=0;k=0; continue; } if(b[0]=='E'&&n!=0){ all[n].end=w; if(all[n].begin!=-1){ sum+=all[n].end-all[n].begin; //all[n].begin=-1;//常理来说需要重置。。 ++k; } } else if(b[0]=='S'&&n!=0){ all[n].begin=w; } } //system("pause"); return 0; }