PAT (Basic Level) Practice 1028

1028 人口普查 (20分)

题目粘过来格式不对就不写题目了,自行查看浙大PTA乙级1028,以后会用Latex后再补充回来题目。
本题属于简单查找类型,我采用C++编译器,版本是g++ 6.5.0

												题解一
#include <cstdio>
struct{
    char name[6];
    int year_mon_day;
}temp,old,young; // 好理解,结构保存名字和时间,因为时间是8位数字,直接用int类型
 
int change(char *year_mon_day){
    return (year_mon_day[0]-'0')*10000000+(year_mon_day[1]-'0')*1000000+(year_mon_day[2]-'0')*100000+(year_mon_day[3]-'0')*10000
        +(year_mon_day[5]-'0')*1000+(year_mon_day[6]-'0')*100+(year_mon_day[8]-'0')*10+year_mon_day[9]-'0';
} // 这个函数用来处理时间,将时间转换为数字形式,感觉我写得有一种强行解题的意味hhh

int main()
{
    int n,year,mon,day,count = 0;
    char year_mon_day[15];
    
    old.year_mon_day = 20140906; // 设置假定最老
    young.year_mon_day = 18140906;
    scanf("%d", &n);
    for(int i = 0;i < n;i++)
    {
        scanf("%s %s", temp.name, year_mon_day);
        if(change(year_mon_day) >= 18140906 && change(year_mon_day) <= 20140906) // 筛查符合条件的数量
        {
            ++count;
            temp.year_mon_day = change(year_mon_day);
            if(temp.year_mon_day < old.year_mon_day) old = temp;
            if(temp.year_mon_day > young.year_mon_day) young = temp;
        }
    }
    if(count == 0) // 特别注意可能没有符合条件的!! 这是一个测试点
        printf("0");
    else
        printf("%d %s %s", count, old.name, young.name);
    
    return 0;
}

思路:1.不采用读入结构数组再排序的方式,容易超时。
2.将对生日的比较转换成对int数据的比较
3.对合法的数据才与old.year_mon_day和young.year_mon_day比较,符合条件则进行结构体直接赋值。
4.千万注意可能出现count = 0也就是没有合法日期的条件下要单独输出,这个测试点分数很高。
时间复杂度为O(n)。

1032 挖掘机技术哪家强 (20分)

题目自行查看PTA乙级
基本思想就是用数组直接保存每个学校的分数,每个下标就是学校ID

#include <cstdio>
const int max = 100001;
int sch[max];
int main(void)
{
    int n,schID,score,max;
    
    scanf("%d", &n);
    for(int i = 0;i < n;i++)
    {
        scanf("%d%d", &schID, &score);
        sch[schID] += score;
    }
    max = 1;
    for(int i = 1;i < n;i++)
    {
        if(sch[max] < sch[i])
            max = i;
    }
    printf("%d %d", max, sch[max]);
    
    return 0;
}

没有时间复杂度讨论的必要,正常时间下不会超时。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值