PAT A1108 Finding Average【状态机】

本文介绍了一种使用状态机判断并过滤非法数值的方法,应用于求平均数问题。通过状态机判断字符串是否为实数,大小是否在[-1000,1000]内,以及小数位数是否超过2位。使用了C++的stod()函数进行字符串到double类型的转换。

这道题的大意是让你求平均数,然而,输入中会包括一些不合法的数,这些数不能参与平均数的计算。这些不合法的数包括根本不是实数的字符串,以及虽然是实数,但是大小不在[-1000,1000]之内的数和小数位数超过2的数。
我这道题很快就做出来了,为什么要写一篇日记记录一下,是因为我在做这道题的时候应用了编译原理课上状态机处理常数的思想,利用状态机先判断当前的字符串是否是实数,如果是,再判断大小。这并不是什么算法知识,但是我个人感觉这种思想以后还会用到,因此记录一下。
除此之外,c++还有一个新特性被我应用了,那就是string直接转换为double的函数:stod(),我在这道题里使用了long double,对应的函数即为stold(),但是其实没必要开long double,我是因为开始WA了几次,想试试换个数据类型能不能过而已。如果是int,也有对应的函数:stoi(),这个函数大大简化了这道题的代码量。

#include <iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<set>
#include<list>
#include<unordered_set>
using namespace  std;
int n;
int num=0;
string s;
char temp[40];
long double ans=0;
bool isDigit(char k)
{
    if(k>='0'&&k<='9')return true;
    return false;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",temp);
        s=temp;
        int l=s.length();
        int state=1;
        bool isReal=true;
        int digit=0;
        for(int j=0;j<l;j++)
        {
            char cur=s[j];
            if(state==1)
            {
                if(cur=='-'||isDigit(cur))
                {
                    state=2;
                }
                else{
                    isReal=false;
                    break;
                }
            }
            else if(state==2)
            {
                if(isDigit(cur))
                {
                    continue;
                }
                else if(cur=='.')
                {
                    state=3;
                }
                else{
                    isReal=false;
                    break;
                }
            }
            else if(state==3)
            {
                if(isDigit(cur))
                {
                    digit++;
                    continue;
                }
                else{
                    isReal=false;
                    break;
                }
            }
        }

        if(isReal==false||digit>2)
        {
            printf("ERROR: %s is not a legal number\n",temp);
            continue;
        }
        else{
            long double w=stold(s);
            if(w>1000||w<-1000)
            {
                printf("ERROR: %s is not a legal number\n",temp);
                continue;
            }
            else{
                ans+=w;
                num++;
            }
        }
    }
    if(num>0)
    {
        ans=ans*1.0/num;
        if(num>1)
        printf("The average of %d numbers is %.2Lf",num,ans);
        else
            printf("The average of %d number is %.2Lf",num,ans);
    }
    else{
        printf("The average of 0 numbers is Undefined");
    }

}
<think>好的,我需要帮助用户找到一个IT相关的示例代码。用户提到了引用[1]中的内容,涉及BuildContext和Element对象,这看起来和Flutter框架有关。首先,我应该回忆一下Flutter中BuildContext的作用,通常它在构建Widget时使用,而Element是树中的实际对象。 接下来,用户需要示例代码。我应该找一个展示如何在Flutter中使用BuildContext的简单例子。StatelessWidget的build方法会自动接收context参数,这个context就是当前的BuildContext。比如,在获取主题数据或导航时常用到。 然后,我需要确保代码符合用户的要求:行内公式用$...$,独立公式用$$...$$,但这里可能不需要数学公式。重点是正确的代码示例和引用。记得在回答末尾添加相关问题,可能涉及BuildContext的其他用途、Element的关系或常见错误。 最后,检查引用是否正确,用户提供的引用[1]是关于BuildContext和Element的关系,所以在提到context时添加引用标识。确保所有内容用中文,结构清晰,分步骤说明,并自然地融入示例。</think>在Flutter应用开发中,`BuildContext`常用于访问Widget树中的元素或服务。以下是获取主题颜色的示例代码: ```dart class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { // 通过BuildContext获取主题颜色 final colorScheme = Theme.of(context).colorScheme; return Container( color: colorScheme.primary, child: Text('使用主题色', style: TextStyle(color: colorScheme.onPrimary)), ); } } ``` 此代码展示了`BuildContext`在获取主题配置时的典型用法,其中`Theme.of(context)`通过上下文定位最近的Theme组件[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值