题意:根据条件判定哪些数是合法的,哪些是不合法的。求其中合法的数的平均值。

思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用scanf()和printf()。另外需要了解atof()函数,用的较多的是atoi(),但这里是浮点数的转换,所以不要搞错了。(两者都在头文件<stdlib.h>下)

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>//atof()
#include <cctype>//isalpha(ch)

bool judge(char str[])
{
    int len=strlen(str);
    ,pointPos=-;
    ;i<len;i++){
        if(str[i]=='.') {
            pointCnt++;
            pointPos=i;
        }
        if(isalpha(str[i])) return false;//如果含有字母
    }
    ) return false;//如果含有多个小数点
     && len--pointPos>) return false;//如果有一个小数点,但小数位数超过2位
    double tmp=atof(str);//到这一步,str已经是个合法的数了,还需判断是否在给定范围内
     || tmp>) return false;
    return true;
}

int main()
{
    //freopen("pat.txt","r",stdin);
    ;
    ];
    ;
    scanf("%d",&n);
    ;i<n;i++){
        scanf("%s",str);
        bool isLegal=judge(str);
        if(isLegal){
            validCnt++;
            sum+=atof(str);
        }else
            printf("ERROR: %s is not a legal number\n",str);
    }//for
    ) printf("The average of 0 numbers is Undefined\n");
    ) printf("The average of 1 number is %.2f\n",sum);
    else printf("The average of %d numbers is %.2f\n",validCnt,sum/validCnt);
    ;
}

1108 Finding Average的更多相关文章

  1. 1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  2. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  3. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  4. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  5. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

  6. PAT 1108 Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  7. PAT Advanced 1108 Finding Average (20 分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  8. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  9. PAT甲级——1108.Finding Average (20分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

随机推荐

  1. scala学习手记27 - 下划线与参数

    在Scala里,下划线(_)可以表示函数值的参数.如果某个参数在函数里仅使用一次,就可以用下划线表示.每次在函数里用下划线,都表示随后的参数. val arr = Array(1, 2, 3, 4, ...

  2. 如果在applicationContext.xml中没有配置bean的属性,那么也会导致空指针异常

    报错如下: java.lang.NullPointerException cn.itcast.action.VisitAction.toAddPage(VisitAction.java:37) sun ...

  3. spring mvc: 密码框

    以user为例,包含username, password字段. user.java public class User { private String username; private Strin ...

  4. 关于Action和EventHandler

    .net框架自带的两个常用类(Action和EventHandler),当然这两个类型的也可以自定义,但系统已经提供,直接拿来用即可,很方便 1:Action : 引用“void方法”的委托,目前框架 ...

  5. 为什么font-size推荐使用具体数值?

    1.font-size的单位 font-size通常用的单位是px/em/rem,px就不说了,em/rem 主要用在移动端,原因的根据根元素大小进行适配,简而言之,em相对于父级定义基础字号,rem ...

  6. lftp使用

    lftp -c 'pget -n 5 ftp://user:password@ftpserver/test.txt'  5 线程数 安装: yum install lftp -y使用语法:lftp - ...

  7. 实现QQ抽屉效果

    代码: #coding: utf-8 from PyQt4.QtCore import * from PyQt4.QtGui import * import sys QTextCodec.setCod ...

  8. 【spark】常用转换操作:keys 、values和mapValues

    1.keys 功能: 返回所有键值对的key 示例 val list = List("hadoop","spark","hive",&quo ...

  9. wc.exe(c语言实现)

    Github项目地址:https://github.com/zhongciting2009/wc WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写 ...

  10. 【python】没有root权限的时候安装Python package

    下载相关位置的包 1.首先在git上下载对应的包: 搜索package github,找到地址.使用 git clone https://xxx.git 命令 2.使用python setup.py ...