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

  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.  习题3-2单词的长度
  70. 输入若干单词,输出它们的平均长度,单词只包括大小写字母,用一个或多个空格隔开
  71. #include<stdio.h>
  72. #include <string.h>
  73. #include<ctype.h>
  74. #define MAXN 1000+10
  75. char buf[MAXN],s[MAXN]; 
  76. int p[MAXN];
  77. int main()
  78. {
  79.     int i,j,m=1,n,k=0;
  80.     int count=0; 
  81.     float averlength;
  82.     fgets(buf,sizeof(s),stdin);//从标准输入设备(就是键盘)读入一串字符,包括空白等符号在内。读入到 字符数组 buf 里,读入最大长度为 s[MAXN]
  83.     n=strlen(buf);
  84. //    printf("n=%d\n",n);
  85.     for(i=0;i<n;i++)
  86.     { 
  87.         if(!isalpha(buf[i]))//判断是否为字母 
  88.         {
  89.         //printf("%d ",i);
  90.         p[k++]=i;
  91.         count+=1;    
  92.         }
  93.     }
  94. //    printf("count=%d\n",count);
  95. //    printf("%d\n",k);
  96.     for(j=0;j<k;j++)
  97.     {
  98.         if((p[j+1]-p[j])>1)
  99.         {
  100.         m+=1;    
  101.         }    
  102.     }
  103.     printf("单词个数=%d\n ",m);
  104.     printf("所有单词的总长度=%d\n ",n-count);    
  105.     averlength=(float)(n-count)/m;
  106.     printf("平均长度=%.3f\n",averlength);
  107.     return 0;    
  108. }
  109. 习题3-3乘积的末三位
  110. 输入若干整数(正负或零),输出它们乘积的末三位.这些整数中会混入一些由大写字母组成的字符串,你的程序应当忽略它们.
  111. #include<string.h>
  112. #include<stdio.h>
  113. int main(){
  114.     char c;
  115.     int s=1;
  116.     int t=1;
  117.     int mark=0;
  118.     while((c=getchar())!=EOF&&c!='\n'){
  119.         if(c>='0'&&c<='9'){
  120.             if(mark!=0){
  121.               t=t*10+(c-'0');
  122.             }
  123.             else
  124.               t=(c-'0');
  125.            mark++;
  126.         }
  127.         else if(c==' '){
  128.            t=t%1000;
  129.             if(mark!=0)
  130.               s=s*t;
  131.             mark=0;
  132.             t=0;
  133.             s=s%1000;
  134.         }
  135.     }
  136.     if(mark!=0)
  137.       s=s*t;
  138.     s=s%1000;
  139.     printf("%ld\n",s);
  140.     return 0;
  141. }
  142. 习题3-4计算器
  143. 编写程序,输入一行恰好包含一个加号,减号,或乘号的表达式,输出他们的值,运算符为二元运算符,运算数不超过100的非负整数.运算数和运算符可以紧挨着,也可以用一个或多个空格隔开
  144. #include <stdio.h>
  145. #include <stdlib.h>
  146. #include <string.h>
  147. #define MAXN 100
  148. char a[MAXN];
  149. int main(int argc, char *argv[])
  150. {
  151.    int i, n, x = 0, y = 0, middle; 
  152.    while(fgets(a, sizeof(a), stdin))
  153.    {
  154.       n = strlen(a);
  155.       for(i = 0; i < n-1; i++)
  156.       {
  157.          if(a[i] == '+' || a[i] == '-' || a[i] == '*')
  158.            middle = i; 
  159.       }
  160.       for(i = 0; i < middle; i++)
  161.       {
  162.          if(a[i] == ' ') continue;
  163.          x = x*10 + a[i] - '0';
  164.       }
  165.       for(i = middle+1; i < n-1; i++ )
  166.       {
  167.           if(a[i] == ' ') continue;
  168.           y = y*10 + a[i] - '0';
  169.       }
  170.       
  171.       switch(a[middle])
  172.       {
  173.       case '+':
  174.            printf("%d\n", x+y);break;
  175.       case '-':
  176.            printf("%d\n", x-y);break;
  177.       case '*':
  178.            printf("%d\n", x*y);break;
  179.       }
  180.       x = 0;
  181.       y = 0;
  182.    }
  183.   system("PAUSE");    
  184.   return 0;
  185. }
  186.                                                                                                                     未完持续......

     

猜你喜欢

转载自blog.csdn.net/a22222259/article/details/87879713