首先,承认这道没有太大的难度,但是有好多值得学习的地方:
我的WA:正确的结果,但是runtime error 的问题;
#include<stdio.h> #include <stdlib.h> #include<string.h> int main() { int N,M; scanf("%d",&N); if (N==0) return 0; while(N--) { scanf("%d",&M); char num[15],tem_num1[15],tem_num2[15]; int h1,m1,s1,h2,m2,s2; int tem=0,tem1=0,tem2=0; for (int i=0;i<M;i++) { scanf("%s %d:%d:%d",&num,&h1,&m1,&s1); 值得学习的输入方法 int tem=h1*3600+m1*60+s1; if (tem1>=tem) { tem1=tem;strcpy(tem_num1,num); } scanf("%d:%d:%d",&h2,&m2,&s2); int tem1=h2*3600+m2*60+s2; if (tem2<=tem1) { tem2=tem1;strcpy(tem_num2,num); } } printf("%s %s\n",tem_num1,tem_num2); } return 0; }
改了很多次,还是WA,只好借鉴大神的了:http://blog.csdn.net/sjf0115/article/details/7701901
思路是一样的,就是用的数组;
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int N,M; //输入重定向,输入数据将从in.txt文件中读取 //freopen("C:\\Users\\SJF\\Desktop\\in.txt","r",stdin); while(scanf("%d",&N)!=EOF) { int i,j; int in = 0,out = 0; int hour,minute,second,inTime[100],outTime[100]; char ID[100][100]; for(i = 0;i<N;i++){ scanf("%d",&M); for(j = 0;j<M;j++){ scanf("%s %d:%d:%d",&ID[j],&hour,&minute,&second); inTime[j] = hour*3600 + minute*60 + second; if(inTime[j] < inTime[in]){ in = j; } scanf("%d:%d:%d",&hour,&minute,&second); outTime[j] = hour*3600 + minute*60 + second; if(outTime[j] > outTime[out]){ out = j; } } printf("%s %s\n",ID[in],ID[out]); } } return 0; }
还有一个c++的简洁的版本:http://www.cnblogs.com/simplelifestyle/p/3761882.html
值得学习::
#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;
时间: 2024-10-05 10:44:11