10.4题解

A 统计元音个数……

#include<stdio.h>  
#include<string.h>  
int main()  
{  
    char str[101];  
    int s,i,j,len,count1,count2,count3,count4,count5;  
    scanf("%d",&s);  
    getchar();  
    for(j=0;j<=s-1;j++)  
    {  
        gets(str);  
        len=strlen(str);  
        count1=count2=count3=count4=count5=0;  
        for(i=0;i<=len-1;i++)  
        {  
            if(str[i]=='a') count1++;  
            if(str[i]=='e') count2++;  
            if(str[i]=='i') count3++;  
            if(str[i]=='o') count4++;  
            if(str[i]=='u') count5++;  
        }  
        printf("a:%d\n",count1);  
        printf("e:%d\n",count2);  
        printf("i:%d\n",count3);  
        printf("o:%d\n",count4);  
        printf("u:%d\n",count5);  
        if(j<s-1)  
        {  
            printf("\n");  
        }  
    }  
    return 0;  
}  

B 按题目描述,那么排好序以后以n/2为起点为最优

#include <stdio.h>  
#include <math.h>  
#include <algorithm>  // 照着打就行,目前不用太了解
using namespace std;  //同上
int main()  
{  
    int m, n, a[501], i, mid, sum;  
    scanf("%d", &m);  
    while (m--)  
    {  
        scanf("%d", &n);  
        for(i = 0; i < n; i++)  
            scanf("%d", &a[i]);  
        sort(a, a+n); // sort(a,a+n) 把 a[0]~a[n-1] 这n个数从小到大排好序 
        mid = a[n/2];  
        sum = 0;  
        for (i = 0; i < n; i++)  
            sum += abs(a[i] - mid);  
        printf("%d\n", sum);  
    }  
    return 0;  
}  

C A+B 问题

#include<stdio.h>
 int main()
 {
      int a,b;
      while(scanf("%d%d",&a,&b)!=EOF)
      printf("%d\n",a+b);
      return 0;
 }

D 找最大元素

#include<stdio.h>  
#include<string.h>  
int main()  
{  
    char max,str[101];  
    int len,i;  
    while(~scanf("%s",str))  
    {  
        len=strlen(str);  
        max='a';  
        for(i=0;i<=len-1;i++)  
        {  
            if(str[i]>max)  
            {  
                max=str[i];  
            }  
        }  
        for(i=0;i<=len-1;i++)  
        {  
            printf("%c",str[i]);  
            if(str[i]==max)  
            {  
                printf("(max)");  
            }  
        }  
        printf("\n");  
    }  
    return 0;  
}  

E 第几天
注意闰年啊
细节看代码…….打代码写注释够详细啦~~·

#include <stdio.h>  

int leapyear_day(int year, int month)  
{  
    // 1月或2月不用加1天,其他月份润年加1天,非润年不用加1天  
    if(month <= 2)  
        return 0;  
    else  
        return ( ((year%4==0) && (year%100!=0)) || (year%400==0) )?1:0;  
}  

int main(void)  
{  
    int year, month, day;  
    int days;  
    int monthsum[] = {  // 到某月份的天数,润年另外加天数  
          0                                 // 1月  
        , 31                                // 2月  
        , 31+28                             // 3月  
        , 31+28+31                          // 4月  
        , 31+28+31+30                       // 5月  
        , 31+28+31+30+31                    // 6月  
        , 31+28+31+30+31+30                 // 7月  
        , 31+28+31+30+31+30+31              // 8月  
        , 31+28+31+30+31+30+31+31           // 9月  
        , 31+28+31+30+31+30+31+31+30        // 10月  
        , 31+28+31+30+31+30+31+31+30+31     // 11月  
        , 31+28+31+30+31+30+31+31+30+31+30  // 12月  
    };  

    while(scanf("%d/%d/%d", &year, &month, &day) != EOF) {  
        // 天数 = 润年需要加的天数(根据年和月计算) + 到某月天数 + 月内天数  
        days = leapyear_day(year, month) + monthsum[month-1] + day;  

        // 输出结果  
        printf("%d\n", days);  
    }  

    return 0;  
}  

F 首字母大写

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
int main()  
{  
    char a[105];  
    int i,len;  
    while(gets(a)!='\0')  
    {  
        len=strlen(a);  
        if(a[0]!=' ')  
            a[0]=a[0]-32;  
        for(i=1;i<len;i++)  
        {  
            if(a[i]!=' '&&a[i-1]==' ')   // 判断是否为首字母
            {  
                a[i]=a[i]-32;     
                 // 小写字母比大写字母的ASCLL码多了32  即 'A'-'a'=32

            }  
        }  
        puts(a);  
    }  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值