题目1013:开门人和关门人

来源:互联网 发布:java三角形面积公式 编辑:程序博客网 时间:2024/05/09 14:23
题目1013:开门人和关门人

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:6259

解决:3155

题目描述:
    每天第一个到机房的人要把门打开,最后一个离开的人要把门关好。现有一堆杂乱的机房签到、签离记录,请根据记录找出当天开门和关门的人。
输入:

    测试输入的第一行给出记录的总天数N ( N> 0 ),下面列出了N天的记录。 
    每天的记录在第一行给出记录的条目数M (M > 0 ),下面是M行,每行的格式为 

    证件号码 签到时间 签离时间 

    其中时间按“小时:分钟:秒钟”(各占2位)给出,证件号码是长度不超过15的字符串。

输出:

    对每一天的记录输出1行,即当天开门和关门人的证件号码,中间用1空格分隔。 
    注意:在裁判的标准测试输入中,所有记录保证完整,每个人的签到时间在签离时间之前,且没有多人同时签到或者签离的情况。

样例输入:
31ME3021112225321 00:00:00 23:59:592EE301218 08:05:35 20:56:35MA301134 12:35:45 21:40:423CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40
样例输出:
ME3021112225321 ME3021112225321EE301218 MA301134SC3021234 CS301133
来源:
2005年浙江大学计算机及软件工程研究生机试真题
答疑:

解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7737-1-1.html


思路:还是比较简单的吧,把第一个人的编号当做开门和关门的,以后读取每个人,然后都和之前的进行比较即可。


#include <iostream>#include <string>#include <fstream>using namespace std;struct Time{string str;//证件号码 int time[6];//表示时间的 };int main(){Time open,close;ifstream in;in.open("1.txt");int n,m;in>>n;while(n--){in>>m;string str1,str2;int count=0;in>>open.str;close.str=open.str;//把第一个人默认为开门和关门的人 in>>str1;//记录第一个人的开门时间 for(int i=0;i<str1.length();i++){if(str1[i]!=':'){open.time[count]=str1[i]-'0';++count;}}count=0;in>>str2;//记录第一个人的关门时间 for(int i=0;i<str2.length();i++){if(str2[i]!=':'){close.time[count]=str2[i]-'0';++count;}}for(int i=1;i<m;i++)//开始循环判断时间的大小,来确定开门和关门的编号  {string str,str1,str2;int count=0;int temp[6];in>>str;//输入证件号 in>>str1;//比较开门的时间 for(int i=0;i<str1.length();i++){if(str1[i]!=':'){temp[count]=str1[i]-'0';++count;}}for(int i=0;i<6;i++){if(temp[i]<open.time[i]){open.str=str;for(int j=0;j<6;j++)open.time[j]=temp[j];break;}else if(temp[i]>open.time[i])break;}count=0;in>>str2;//比较关门的时间 for(int i=0;i<str2.length();i++){if(str2[i]!=':'){temp[count]=str2[i]-'0';++count;}}for(int i=0;i<6;i++){if(temp[i]>close.time[i]){close.str=str;for(int j=0;j<6;j++)close.time[j]=temp[j];break;}else if(temp[i]<close.time[i])break;}}cout<<open.str<<" "<<close.str<<endl;}return 0;} 

(2)看了一下别人的,竟然还是用字符串比较,的确很方便。


#include <iostream>  #include <stdio.h>  #include <string.h>  #include <string>  #include <algorithm>  using namespace std;  const int maxn = 10002;  struct Task{      char name[16];      char start[12];      char end[12];  }tasks[maxn];  Task minTask;  Task maxTask;  int n,m,i,j;   int main(){      char start[12];      char end[12];      char name[16];      cin >> n;           for(i = 0; i < n ; i++){          cin >> m;          cin >> minTask.name;          cin >> minTask.start;          cin >> minTask.end;          strcpy(maxTask.name,minTask.name);          strcpy(maxTask.start,minTask.start);          strcpy(maxTask.end,minTask.end);          for(j = 1; j < m ; j++){              cin >> name;              cin >> start;              cin >> end;              if(strcmp(start,minTask.start) < 0){                  strcpy(minTask.name,name);                  strcpy(minTask.start,start);                  strcpy(minTask.end,end);              }                 if(strcmp(end,minTask.end) > 0){                  strcpy(maxTask.name,name);                  strcpy(maxTask.start,start);                  strcpy(maxTask.end,end);              }                  }          cout<<minTask.name<<' '<<maxTask.name<<endl;      }      return 0;  }   
(3)还有更神奇的字符串比较函数compare()的使用,新技能get.


#include<iostream>  #include<string>  using namespace std;  int main()  {      int n;      cin>>n;      for(int j=0;j<n;j++)      {          int count=0;          cin>>count;          string str[10000][3]={""};          int min=0,max=0;          cin>>str[0][0]>>str[0][1]>>str[0][2];          for(int i=1;i<count;i++)          {              cin>>str[i][0]>>str[i][1]>>str[i][2];              if(str[i][1].compare(str[min][1])<0)min=i;              if(str[i][2].compare(str[max][2])>0)max=i;          }          cout<<str[min][0]<<' '<<str[max][0]<<endl;      }      return 0;  }  

compare函数用来进行字符串以及其子串的比较,示例如下:

#include <iostream>  #include <string>  #include <cctype>  using std::cout;  using std::endl;  using std::cin;  using std::string;  int main(void){      string str1="hi,test,hello";      string str2="hi,test";      //字符串比较      if(str1.compare(str2)>0)          printf("str1>str2\n");      else if(str1.compare(str2)<0)          printf("str1<str2\n");      else          printf("str1==str2\n");            //str1的子串(从索引3开始,包含4个字符)与str2进行比较      if(str1.compare(3,4,str2)==0)          printf("str1的指定子串等于str2\n");      else          printf("str1的指定子串不等于str2\n");            //str1指定子串与str2的指定子串进行比较      if(str1.compare(3,4,str2,3,4)==0)          printf("str1的指定子串等于str2的指定子串\n");      else          printf("str1的指定子串不等于str2的指定子串\n");            //str1指定子串与字符串的前n个字符进行比较      if(str1.compare(0,2,"hi,hello",2)==0)          printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");      else          printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");      return 0;        }  



0 0
原创粉丝点击