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

思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用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. grunt,gulp,webpack前端打包工具的特性

    1.http://www.cnblogs.com/lovesong/p/6413546.html (gulp与webpack的区别) 2.http://blog.csdn.net/qq_3231263 ...

  2. html03表单

    <!DOCTYPE HTML> <html> <head> <title>用户登录的表单</title> </head> < ...

  3. sql语句遇到错误: The used SELECT statements have a different number of columns :

    这是因为使用union的两个SQL语句产生的记录的表结构不一致. 必须是结构完全一致的记录集合才可以使用UNION. 以上就是两个表的字段不一样,导致,所以大家可以检查下.

  4. CSS: Flexbox

    Use flexbox to create a responsive website, containing a flexible navigation bar and flexible conten ...

  5. 初识Linux(五)--VI/VIM编辑器

    我们操作文件,终究离不开编辑文件,对文件内容的编辑,Linux系统下,我们通常使用VI/VIM来编辑文件.VI是每个Linux都会自带的文本编辑器,VIM是VI的增强版,可能有些发行版本没有自带,可以 ...

  6. 老师木发的makefile与autotools

    makefile http://scc.qibebt.cas.cn/docs/linux/base/%B8%FA%CE%D2%D2%BB%C6%F0%D0%B4Makefile-%B3%C2%F0%A ...

  7. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\....\App_Web_default.aspx.cdcab7d2.zii776dc.dll”--“拒绝访问。 ”

    关于访问asp.net站点出现以下问题的解决办法: 问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Tempora ...

  8. 转载关于reset vector 和 exception vector

    在NIOS II学习过程中设置CPU参数的时候,遇到Reset Vector和Exception Vector的设置.参数设置画面如下图所示. Reset Vector——复位向量Exception ...

  9. 【spring源码学习】spring的远程调用实现源码分析

    [一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...

  10. 转:django在生成数据库时常常遇到的问题

    真的很有用! http://blog.csdn.net/pipisorry/article/details/45727309