貼貼貼。
沒技巧:
#include <bits/stdc++.h>
using namespace std;
struct node {
int year;
int month;
int day;
string name;
} s[100010];
bool cmp(node n1,node n2) {
if(n1.year!=n2.year) return n1.year < n2.year;
if(n1.month!=n2.month) return n1.month<n2.month;
return n1.day<n2.day;
}
int main() {
int n;
cin>>n;
string sname;
int count = 0;
int year,month,day;
for(int i=0; i<n; i++) { //合法生日在1814/9/6到2014/9/6之間
cin>>sname;
scanf("%d/%d/%d",&year,&month,&day);
//是否合法
if(year > 2014) continue;//非法
if(year == 2014) {
if(month > 9) continue;
if(month == 9 && day > 6) {
continue;
}
}
if(year < 1814) continue;
if(year == 1814) {
if(month < 9) continue;
if(month==9 && day < 6) {
continue;
}
}
//合法生日
count++;
s[count].day=day;
s[count].month=month;
s[count].year=year;
s[count].name = sname;
}
sort(s+1,s+1+count,cmp);
cout << count;
if(count != 0) {
cout << " " << s[1].name << " "<<s[count].name;
}
return 0;
}
其他做法:
用一個long long型變數 = year10000+month100+day; 這樣兩個日期就可以直接比較。
#include <bits/stdc++.h>
using namespace std;
struct DATA{
char name[10];
char birth[10+5];
};
vector<DATA> arr;
const char date1[] = "2014/09/06";
const char date2[] = "1814/09/06";
bool cmp(const DATA &a, const DATA &b){
return strcmp(a.birth, b.birth) < 0;
}
int main(){
int n;
DATA tmp;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%s %s", tmp.name, tmp.birth);
if(strcmp(tmp.birth, date1) > 0){
continue;
}
if(strcmp(tmp.birth, date2) < 0){
continue;
}
arr.push_back(tmp);
}
sort(arr.begin(), arr.end(), cmp);
if(arr.size() != 0){
printf("%d %s %s\n", arr.size(), arr[0].name, arr[arr.size()-1].name);
}
else{
printf("0");
}
return 0;
}