zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典 第三章 习题

    习题3-1 分数统计:输入一些学生的分数,哪个分数出现的次数最多?如果有多个并列,从小到大输出。

    任务1:分数均为不超过100的非负整数

     1 //习题3.1,分数统计(stat)
     2 #define LOCAL
     3 #include<stdio.h>
     4 #include<string.h>
     5 #ifndef MAX
     6 #define MAX 100+1
     7 #endif 
     8 
     9 int a[MAX];
    10 int main(){
    11 
    12     //从本地读取文件(重定向),不用每次都进行数据输入
    13     #ifdef LOCAL
    14     freopen("data.txt","r",stdin);
    15     #endif
    16 
    17     memset(a,0,sizeof(a));
    18 
    19     int degree;
    20     while(scanf("%d",&degree) == 1){
    21         a[degree] += 1;
    22     }
    23 
    24     int i,max = a[0];
    25     int tmp[MAX];
    26      memset(tmp,0,sizeof(tmp));
    27 
    28      for(i=1; i <= MAX; i++){
    29          if(a[i] > max){
    30              max = a[i];
    31              }
    32          }
    33     int j = 0;
    34     for(i = 0; i < MAX; i++){
    35         if(a[i] == max){
    36             tmp[j] = i;
    37             j++;
    38             }
    39     }
    40 
    41     for (i = 0; i < j; ++i)
    42     {
    43         printf("%d
    ",tmp[i]);
    44     }
    45     return 0;
    46 }

    任务2:分数均为不超过100的非负实数,但最多保留两位小数

     1 //习题3.1,分数统计(stat)
     2 #define LOCAL
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<math.h>
     6 #ifndef MAX
     7 #define MAX 10000+1
     8 #endif 
     9 
    10 int a[MAX];
    11 int main(){
    12     //从本地读取文件(重定向),不用每次都进行数据输入
    13     #ifdef LOCAL
    14     freopen("data.txt","r",stdin);
    15     #endif
    16 
    17     memset(a,0,sizeof(a));
    18 
    19     double degree;
    20     while(scanf("%lf",&degree) == 1){
    21         //直接double强制转化为int会出现问题,如4.9999999999,应为5,但会是4.9
    22         //使用floor进行四舍五入可以解决这个问题
    23         double m = degree * 100;
    24         int n = floor(m+0.5);
    25         a[n] += 1;
    26     }
    27 
    28     int i,max = a[0];
    29     int tmp[MAX];
    30     memset(tmp,0,sizeof(tmp));
    31 
    32     for(i=1; i <= MAX; i++){
    33         if(a[i] > max){
    34             max = a[i];
    35         }
    36     }
    37 
    38     int j = 0;
    39     for(i = 0; i < MAX; i++){
    40         if(a[i] == max){
    41             tmp[j] = i;
    42             j++;
    43         }
    44     }
    45 
    46     for (i = 0; i < j; ++i)
    47     {
    48         double temp = tmp[i]*0.01;
    49         printf("%.2f
    ",temp);
    50     }
    51     
    52     return 0;
    53 }

     习题3-2:单词的长度(word):输入若干个单词,输出它们的平均长度。单词只包含大写字母和小写字母,用一个或多个空格隔开

     1 #define LOCAL
     2 #include<stdio.h>
     3 #include<ctype.h>
     4 #define MAX 1000
     5 char a[MAX];
     6 
     7 int main(){
     8     #ifdef LOCAL
     9     freopen("data.txt","r",stdin);
    10     #endif
    11 
    12     char c;
    13     int charNum = 0,wordNum = 1;
    14     int i = 0;
    15 
    16     while(scanf("%c",&c)==1){
    17         
    18         if(c == '
    '){
    19             break;
    20         }else{
    21             a[i] = c;
    22             i++;
    23         }
    24     }
    25 
    26     int j;
    27     for(j = 0; j < i; j++){
    28         if(a[j] == ' '&& isalpha(a[j+1])){
    29             wordNum++;
    30         }
    31         if(isalpha(a[j])){
    32             charNum++;
    33         }
    34     }
    35 
    36     double avg = (1.0)*charNum/wordNum;
    37     printf("%d %d
    ", charNum,wordNum);
    38     printf("%.2lf
    ",avg);
    39     return 0;
    40 }
  • 相关阅读:
    Extjs Google的Suggest的自动提示 从后台取数据
    vue 使用gojs绘制简单的流程图
    网络流24题の详解
    Codeforces Round #587 (Div. 3) F. WiFi(dp+线段树)
    Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(dfs)
    Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置
    MySQL + Amoeba 负载均衡、主从备份方案
    SubVersion(SVN)的安装配置使用
    Tomcat Https配置
    Eclipse 常用快捷键清单
  • 原文地址:https://www.cnblogs.com/fyymonica/p/3681307.html
Copyright © 2011-2022 走看看