zoukankan      html  css  js  c++  java
  • HDU 1235 统计同成绩学生人数

    http://acm.hdu.edu.cn/showproblem.php?pid=1235

    刚开始没有注意题目。效率不高。

    数组直接开到N了。

    View Code
    #include <stdio.h>
    #define MAX_ARRAY 1000
    int main()
    {

    int scores[MAX_ARRAY];
    int nTotal;
    int focus;
    int i;
    while(scanf("%d",&nTotal) &&nTotal)
    {

    for(i = 0; i < nTotal; ++i)
    {
    scanf("%d",&scores[i]);
    }
    scanf("%d",&focus);
    int count = 0;
    for(i = 0 ; i < nTotal ; ++i)
    if(scores[i] == focus)
    ++count;
    printf("%d\n",count);
    }
    }

    后来再想了下,发现可以用hash。

    View Code
    #include <stdio.h>
    #include <memory.h>
    #define MAX_ARRAY 101
    int main()
    {

    int scores[MAX_ARRAY];
    int nTotal;
    int n;
    int i;
    while(scanf("%d",&nTotal) &&nTotal)
    {
    memset(scores,0,sizeof(scores));
    for(i = 0; i < nTotal; ++i)
    {
    scanf("%d",&n);
    ++scores[n];
    }
    scanf("%d",&n);
    printf("%d\n",scores[n]);
    }
    }

    于是MAX_ARRAY 只开到101了。



    两个都AC了,运行时间一样。

  • 相关阅读:
    delete、truncate、drop的区别
    Java闭包
    visio 画网格图
    GPU服务器中了木马病毒
    visio 同时标注上下标
    自动缩放字体
    latex 图片
    多GPU训练
    texstudio 外部查看器错误
    Linux lsof命令详解
  • 原文地址:https://www.cnblogs.com/westfly/p/2402926.html
Copyright © 2011-2022 走看看