刘汝佳/算法竞赛入门经典第三章数组和字符串-上

  1. 习题3-1 分数统计
  2. 输入一些学生的分数,那个分数出现的次数最多?如果有多个并列,从小到大输出.
  3. #include<stdio.h>
  4. #include <string.h>
  5. int main()
  6. {
  7.     int a[10],b[10],count[10];
  8.     int max;
  9.     memset(count,0,sizeof(count));
  10.     int i,j;
  11.     printf("请输入10个学生的分数\n"); 
  12.     for(i=0;i<10;i++)
  13.     {
  14.         scanf("%d",&a[i]);
  15.         b[i]=a[i];
  16.     }
  17.     for(j=0;j<10;j++)
  18.     {
  19.         for(i=0;i<10;i++)
  20.         {
  21.             if(a[j]==b[i])
  22.             {
  23.             count[j]+=1;
  24.             }    
  25.         }
  26.         //printf("第%d个数%d出现的次数为%d\n",j+1,a[j],count[j]); 
  27.     }
  28.     max=count[0];
  29.     for(i=1;i<10;i++)
  30.     {
  31.         if(count[i]>=max)
  32.         {
  33.             max=count[i];
  34.         }
  35.     }
  36.     
  37.     int temp[10];
  38.     for(i=0;i<10;i++)
  39.     {
  40.         temp[i]=-1;    
  41.     }
  42.     int k=0;
  43.     for(i=0;i<10;i++)
  44.     {
  45.         if(max==count[i])
  46.         {
  47.         temp[k++]=a[i];    
  48.         }    
  49.     } 
  50.     for(i=0;i<k-1;i++)
  51.     {
  52.     //排序 
  53.         for(j=0;j<k-1-i;j++)
  54.         {
  55.             if(temp[j]>temp[j+1])
  56.             {
  57.                 int t=temp[j+1];
  58.                 temp[j+1]=temp[j];
  59.                 temp[j]=t;
  60.             }
  61.         }
  62.     }
  63.     for(i=0;i<k;i++)
  64.     printf("%d出现的次数最多,次数为%d\n",temp[i],max);
  65.     return 0;
  66. }
  67. 运行结果:
  68.  
  69. 刘汝佳/算法竞赛入门经典第三章数组和字符串-上
  70.  习题3-2单词的长度
  71. 输入若干单词,输出它们的平均长度,单词只包括大小写字母,用一个或多个空格隔开
  72. #include<stdio.h>
  73. #include <string.h>
  74. #include<ctype.h>
  75. #define MAXN 1000+10
  76. char buf[MAXN],s[MAXN]; 
  77. int p[MAXN];
  78. int main()
  79. {
  80.     int i,j,m=1,n,k=0;
  81.     int count=0; 
  82.     float averlength;
  83.     fgets(buf,sizeof(s),stdin);//从标准输入设备(就是键盘)读入一串字符,包括空白等符号在内。读入到 字符数组 buf 里,读入最大长度为 s[MAXN]
  84.     n=strlen(buf);
  85. //    printf("n=%d\n",n);
  86.     for(i=0;i<n;i++)
  87.     { 
  88.         if(!isalpha(buf[i]))//判断是否为字母 
  89.         {
  90.         //printf("%d ",i);
  91.         p[k++]=i;
  92.         count+=1;    
  93.         }
  94.     }
  95. //    printf("count=%d\n",count);
  96. //    printf("%d\n",k);
  97.     for(j=0;j<k;j++)
  98.     {
  99.         if((p[j+1]-p[j])>1)
  100.         {
  101.         m+=1;    
  102.         }    
  103.     }
  104.     printf("单词个数=%d\n ",m);
  105.     printf("所有单词的总长度=%d\n ",n-count);    
  106.     averlength=(float)(n-count)/m;
  107.     printf("平均长度=%.3f\n",averlength);
  108.     return 0;    
  109. }
  110. 刘汝佳/算法竞赛入门经典第三章数组和字符串-上
  111. 习题3-3乘积的末三位
  112. 输入若干整数(正负或零),输出它们乘积的末三位.这些整数中会混入一些由大写字母组成的字符串,你的程序应当忽略它们.
  113. #include<string.h>
  114. #include<stdio.h>
  115. int main(){
  116.     char c;
  117.     int s=1;
  118.     int t=1;
  119.     int mark=0;
  120.     while((c=getchar())!=EOF&&c!='\n'){
  121.         if(c>='0'&&c<='9'){
  122.             if(mark!=0){
  123.               t=t*10+(c-'0');
  124.             }
  125.             else
  126.               t=(c-'0');
  127.            mark++;
  128.         }
  129.         else if(c==' '){
  130.            t=t%1000;
  131.             if(mark!=0)
  132.               s=s*t;
  133.             mark=0;
  134.             t=0;
  135.             s=s%1000;
  136.         }
  137.     }
  138.     if(mark!=0)
  139.       s=s*t;
  140.     s=s%1000;
  141.     printf("%ld\n",s);
  142.     return 0;
  143. }
  144. 刘汝佳/算法竞赛入门经典第三章数组和字符串-上
  145. 习题3-4计算器
  146. 编写程序,输入一行恰好包含一个加号,减号,或乘号的表达式,输出他们的值,运算符为二元运算符,运算数不超过100的非负整数.运算数和运算符可以紧挨着,也可以用一个或多个空格隔开
  147. #include <stdio.h>
  148. #include <stdlib.h>
  149. #include <string.h>
  150. #define MAXN 100
  151. char a[MAXN];
  152. int main(int argc, char *argv[])
  153. {
  154.    int i, n, x = 0, y = 0, middle; 
  155.    while(fgets(a, sizeof(a), stdin))
  156.    {
  157.       n = strlen(a);
  158.       for(i = 0; i < n-1; i++)
  159.       {
  160.          if(a[i] == '+' || a[i] == '-' || a[i] == '*')
  161.            middle = i; 
  162.       }
  163.       for(i = 0; i < middle; i++)
  164.       {
  165.          if(a[i] == ' ') continue;
  166.          x = x*10 + a[i] - '0';
  167.       }
  168.       for(i = middle+1; i < n-1; i++ )
  169.       {
  170.           if(a[i] == ' ') continue;
  171.           y = y*10 + a[i] - '0';
  172.       }
  173.       
  174.       switch(a[middle])
  175.       {
  176.       case '+':
  177.            printf("%d\n", x+y);break;
  178.       case '-':
  179.            printf("%d\n", x-y);break;
  180.       case '*':
  181.            printf("%d\n", x*y);break;
  182.       }
  183.       x = 0;
  184.       y = 0;
  185.    }
  186.   system("PAUSE");    
  187.   return 0;
  188. }
  189.                                                                                                                     未完持续......