问题描述
我用Go写的算法,时间复杂度都已经降到了最低,但仍然不能通过PTA一些问题的大规模数据测试点,比如乙级测试题1028的第四个测试点,使用Go语言编写的代码就无法通过。
代码附在尾部。通过使用C语言改写算法,成功通过测试,C代码亦附在尾部。**我猜测可能会是Go fmt函数库运行效率比较慢,**不知道是不是这个原因,情各位大佬帮忙指点。
Go代码:
package main
import "fmt"
func main(){
N,S := 0,0
temp_year,max_year,min_year := 0,0,2015
temp_month,max_month,min_month := 0,0,12
temp_day,max_day,min_day := 0,0,12
temp_name,max_name,min_name := "","",""
fmt.Scanf("%d",&N)
for i:=0;i<N;i++{
fmt.Scanf("%s %d/%d/%d",&temp_name,&temp_year,&temp_month,&temp_day)
if temp_year > 2014{
continue
}else if temp_year == 2014{
if temp_month > 9{
continue
}else if temp_month == 9{
if temp_day > 6{
continue
}
}
}
if 2014 - temp_year > 200{
continue
}else if 2014 - temp_year == 200{
if temp_month < 9 {
continue
}else if temp_month == 9{
if temp_day < 6{
continue
}
}
}
if temp_year>max_year{
max_year = temp_year
max_month= temp_month
max_day = temp_day
max_name = temp_name
}else if temp_year == max_year{
if temp_month > max_month{
max_year = temp_year
max_month= temp_month
max_day = temp_day
max_name = temp_name
}else if temp_month == max_month{
if temp_day > max_day{
max_year = temp_year
max_month= temp_month
max_day = temp_day
max_name = temp_name
}
}
}
if temp_year<min_year{
min_year = temp_year
min_month= temp_month
min_day = temp_day
min_name = temp_name
}else if temp_year == min_year{
if temp_month < min_month{
min_year = temp_year
min_month= temp_month
min_day = temp_day
min_name = temp_name
}else if temp_month == min_month{
if temp_day < min_day{
min_year = temp_year
min_month = temp_month
min_day = temp_day
min_name = temp_name
}
}
}
S++
}
if S == 0{
fmt.Print("0")
}else{
fmt.Printf("%d %s %s",S,min_name,max_name)
}
}
C代码:
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main(){
int N=0,S=0,i;
int temp_year,max_year=0,min_year=2015;
int temp_month,max_month=0,min_month = 12;
int temp_day,max_day=0,min_day =31;
char temp_name[6],max_name[6],min_name[6];
scanf("%d",&N);
for (i=0;i<N;i++){
scanf("%s %d/%d/%d",&temp_name,&temp_year,&temp_month,&temp_day);
if (temp_year > 2014){
continue;
}else if (temp_year == 2014){
if (temp_month > 9){
continue;
}else if (temp_month == 9){
if (temp_day > 6){
continue;
}
}
}
if (2014 - temp_year > 200){
continue;
}else if (2014 - temp_year == 200){
if (temp_month < 9) {
continue;
}else if (temp_month == 9){
if (temp_day < 6){
continue;
}
}
}
if (temp_year>max_year){
max_year = temp_year;
max_month= temp_month;
max_day = temp_day;
memcpy(max_name,temp_name,strlen(temp_name)+1);
}else if (temp_year == max_year){
if (temp_month > max_month){
max_year = temp_year;
max_month= temp_month;
max_day = temp_day;
memcpy(max_name,temp_name,strlen(temp_name)+1);
}else if (temp_month == max_month){
if (temp_day > max_day){
max_year = temp_year;
max_month= temp_month;
max_day = temp_day;
memcpy(max_name,temp_name,strlen(temp_name)+1);
}
}
}
if (temp_year<min_year){
min_year = temp_year;
min_month= temp_month;
min_day = temp_day;
memcpy(min_name,temp_name,strlen(temp_name)+1);
}else if (temp_year == min_year){
if (temp_month < min_month){
min_year = temp_year;
min_month= temp_month;
min_day = temp_day;
memcpy(min_name,temp_name,strlen(temp_name)+1);
}else if (temp_month == min_month){
if (temp_day < min_day){
min_year = temp_year;
min_month = temp_month;
min_day = temp_day;
memcpy(min_name,temp_name,strlen(temp_name)+1);
}
}
}
S++;
}
if (S == 0){
printf("0");
}else{
printf("%d %s %s",S,min_name,max_name);
}
return 0;
}