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

思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用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. JSP报错01

    ZC: 创建一个JSP之后,就报如下错误. 1.错误: 1.1. 2.处理: Exlipse for JEE --> Project Explorer 中选择项目 --> 右击项目名 -- ...

  2. hive 导出数据到本地

    有时候需要将hive库中的部分数据导入至本地,这样子做可视化和小规模的数据挖掘实验都是比较方便的.数据导入至本地的HQL语法如下: INSERT OVERWRITE [LOCAL] DIRECTORY ...

  3. JavaEE 技术体系

    JavaEE 技术体系总结: 一:常见模式与工具 设计模式,流行的框架与组件 常见的设计模式,编码必备 Spring5,做应用必不可少的最新框架 MyBatis,玩数据库必不可少的组件 二:工程化与工 ...

  4. 第三方库PIL简单使用

    PIL为第三方库,需要简单安装,最容易的安装方法 pip install PIL 详细内容见http://effbot.org/imagingbook/ 下面展示一个简单用例:(字母验证码简单实现) ...

  5. Django进阶Template篇002 - 模板包含和继承

    包含 {% include %} 允许在模板中包含其他模板的内容. {% include "foo/bar.html" %} {% include template_name %} ...

  6. jenkins构建中的除零错误

    一. 除零错误(ZeroDivisionError) 今天在jenkins上运行接口自动化测试任务时,从控制台输出中看到了除零错误,大概是这样的 从上图中,通过分析,可以得出三个结论: 1. jenk ...

  7. 三十五 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy分布式爬虫要点

    1.分布式爬虫原理 2.分布式爬虫优点 3.分布式爬虫需要解决的问题

  8. Spring 依赖注入(一、注入方式)

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  9. WMS专业名词解释

    1.摘果:按照单一客户上订单的内容进行拣选货品(即去货位上拣货),拣选完成后即可直接进行质检.包装. 2.播种:将多个客户订单上的货品进行汇总,然后对这些货品进行拣选.拣选完成后,再区分出每一个客户的 ...

  10. [置顶] Android Shape一些新玩法?

    敏少咨讯: 1.生活琐事篇 最近由于公司赶项目所以偷懒了,博客没有及时更新,还请小伙伴们手下留情啊!最近发生了很多趣事,就在今天我们学校退书籍费,这可把我开心的哈哈!无缘无故又有钱了,嘿嘿,刚好五一出 ...