problem

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:

SC3021234 CS301133

tip

anwser

#include <bits/stdc++.h>
using namespace std;

string firstName, lastName, firstTime, lastTime;

bool CompTime(int a1, int b1, int c1, int a2, int b2, int c2){
	if (a1 == a2){
		if(b1 == b2)
			return c1 < c2;
		else return b1 < b2;
	}else return a1 < a2;
}

bool CalTime(string timeA, string timeB){
	int a1, a2, b1, b2, c1, c2;
	int tt;
	a1 = (timeA[0]-48)*10 + timeA[1]-48;
	a2 = (timeB[0]-48)*10 + timeB[1]-48;
	
	b1 = (timeA[3]-48)*10 + timeA[4]-48;
	b2 = (timeB[3]-48)*10 + timeB[4]-48;
	
	c1 = (timeA[6]-48)*10 + timeA[7]-48;
	c2 = (timeA[6]-48)*10 + timeA[7]-48;
	
	return CompTime(a1, b1, c1, a2, b2, c2);
}

int main()
{
//	freopen("test.txt", "r", stdin);
	int N;
	cin>>N; 
	string x, y, z;
	for(int i = 0; i < N; i++){
		cin>>x>>y>>z;
//		cout<<x<<y<<z<<endl; 
		if(i == 0) {
			firstName = lastName = x;
			firstTime = y;
			lastTime = z;
		}else{
			if(CalTime(y, firstTime)){
				firstTime = y;
				firstName = x;
			}
			if(CalTime(lastTime, z)){
				lastTime = z;
				lastName = x;
			}
		}
	}
	
	cout<<firstName<<" "<<lastName;
	return 0;
}

/*
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
*/

experience

string 函数 :

// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by asterisks.");
  std::size_t found = str.find_first_of("aeiou");
  while (found!=std::string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}
// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}
// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}
posted on 2018-07-04 22:15  yoyo_sincerely  阅读(282)  评论(0编辑  收藏  举报