Love丶FFC's Blog

PAT乙级:1038 统计同成绩学生

2020-02-29 12:07:04
阅读:724   •   评论:9
标签:,

本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。

输入格式:

输入在第 1 行给出不超过 10​5​​ 的正整数 N,即学生总人数。随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔。最后一行给出要查询的分数个数 K(不超过 N 的正整数),随后是 K 个分数,中间以空格分隔。

输出格式:

在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空格分隔,但行末不得有多余空格。

输入样例:

10
60 75 90 55 75 99 82 90 75 50
3 75 90 88

输出样例:

3 2 0

编程语言:Python、C

解题思想(Python):

1.Python直接使用count方法进行查询

但使用Python会导致最后一个测试点超时

一开始认为超时是因为Python性能的缘故,因此尝试了Java、C语言使用双层循环计数的方法,但是仍然超时

解题思想(C):

1.突然想到一个办法,何不直接创建一个100长度的数组,每输入一个成绩就将对应下标的元素值+1

这样就省去了查询的时间,直接输出查询的成绩对应下标的数组元素即可,使用这个方法大大提高了效率!

时间复杂度:两个算法均O(N)

代码如下(Python):

  1. NOS = int(input()) # 输入成绩个数,Number Of Students
  2. Score = list(map(int, input().split())) # 存放学生成绩
  3. Query = list(map(int, input().split())) # 存放查询次数、具体查询的成绩
  4. NOQ = Query[0] # 将查询次数单独存放,便于后续操作,Number Of Queries
  5. Query.pop(0) # 将查询次数从列表从删除,只保留具体查询的成绩,便于后续操作
  6.  
  7. for i in range(NOQ - 1): # 为了保证输出没有多余的空格,只查询n-1个成绩
  8. print(Score.count(Query[i]), end=' ')
  9. print(Score.count(Query[len(Query) - 1])) # 查询第n个成绩

代码如下(C):

  1. #include<stdio.h>
  2.  
  3. int main(void){
  4. int Frequency[100];//创建一个数组,存放0-100分每个成绩的个数
  5. for(int i=0;i<100;i++)
  6. {
  7. Frequency[i]=0;//数组初始化,将每个成绩的个数默认为0
  8. }
  9. int NOS;//成绩个数
  10. scanf("%d",&NOS);
  11. int Score;
  12. for(int i=0;i<NOS;i++)
  13. {
  14. scanf("%d",&Score);//输入成绩
  15. Frequency[Score]++;//将成绩对应下标的数组元素+1,例如76分,则Frequency[76]++
  16. }
  17. int NOQ;//查询成绩的个数
  18. scanf("%d",&NOQ);
  19. int Query[NOQ];
  20. for(int i=0;i<NOQ;i++)
  21. {
  22. scanf("%d",&Query[i]);//输入查询的成绩
  23. }
  24. for(int i=0;i<NOQ-1;i++)//输出前n-1个成绩对应的次数
  25. {
  26. printf("%d ",Frequency[Query[i]]);
  27. }
  28. printf("%d",Frequency[Query[NOQ-1]]);//输出第n个成绩对应的次数
  29. }

评论板

共有 9 条评论

  1. cheap louis vuitton online

    I just wanted to thank you for the fast service. alternatively they look great. I received them a day earlier than expected. for example the I will definitely continue to buy from this site. regardless I will recommend this site to my friends. Thanks!
    cheap louis vuitton online https://www.cheapreallouisvuitton.com/

  2. cheap jordan shoes

    I just wanted to thank you for the fast service. as well as they look great. I received them a day earlier than expected. cherish the I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
    cheap jordan shoes https://www.cheaprealjordan.com/

  3. cheap jordans online

    I just wanted to thank you for the fast service. alternatively they look great. I received them a day earlier than expected. including I will definitely continue to buy from this site. manner in which I will recommend this site to my friends. Thanks!
    cheap jordans online https://www.realjordansretro.com/

  4. cheap louis vuitton handbags

    I just wanted to thank you for the fast service. while well as they look great. I received them a day earlier than expected. which includes the I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.louisvuittonsoutletstore.com/

  5. cheap louis vuitton handbags

    I just wanted to thank you for the fast service. or perhaps they look great. I received them a day earlier than expected. for example the I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.bestlouisvuittonoutlet.com/

  6. cheap jordans online

    Read reviews and was a little hesitant since I had already inputted my order. and even but thank god, I had no issues. for example the received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans online https://www.cheapjordanssneakers.com/

  7. FoorryNox

    non dairy products, 25 studies cialis buy I had a bilateral There should also be more discussion about self image following breast cancer surgery

  8. Preliaf

    PMID 8769998 finasteride 5 mg generic tablets One substitution was in codon 191, ACA threonine rather than GCA alanine, and the other was in codon 283, TTT phenylalanine instead of TTG leucine

  9. yZptQT

    However, RER was significantly lower in HFD fed BIBO3304 treated mice compared to vehicle treated control mice in both, the light and the dark phases Fig cialis 40 mg Despite the persistence of significant disparities, few evaluations examine disparities in laboratory testing by race ethnicity, age, sex, Medicaid eligibility, and number of chronic conditions for Medicare fee for service beneficiaries newly prescribed medications

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:510461