Stdboy 2020-01-02 11:53
浏览 174

使用Golang在做PTA题目上容易超时

问题描述

我用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;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办