一个算法题目,初中信息学奥赛的复赛题

简介:
题设:给入一个字符串,对其中的数字求和。
    例:输入:acb1.2afr76fdrewfdoc32        输出:109.2(1.2+76+32)
            输入:5.5.5        输出:10.5(5.5+5)

答案(指定用C实现):
None.gif char inputString[100];
None.gif int isNumber( char c)
ExpandedBlockStart.gif {
InBlock.gif    return c>'0'&&c<'9';
ExpandedBlockEnd.gif}

None.gif int isPoint( char c)
ExpandedBlockStart.gif {
InBlock.gif    return c=='.';
ExpandedBlockEnd.gif}

None.gif int getRangeEnd( int cursor)
ExpandedBlockStart.gif {
InBlock.gif    int hasPoint=0;
InBlock.gif    int isStillNumber=1;
InBlock.gif    while(isStillNumber)
ExpandedSubBlockStart.gif    {
InBlock.gif        cursor++;
InBlock.gif        if(hasPoint)
ExpandedSubBlockStart.gif        {
InBlock.gif            isStillNumber=isNumber(inputString[cursor]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            if(isNumber(inputString[cursor]))
ExpandedSubBlockStart.gif            {
InBlock.gif                isStillNumber=1;
InBlock.gif                continue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            if(isPoint(inputString[cursor]))
ExpandedSubBlockStart.gif            {
InBlock.gif                hasPoint=1;
InBlock.gif                isStillNumber=1;
InBlock.gif                continue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            isStillNumber=0;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    return cursor;
ExpandedBlockEnd.gif}

None.gif int getPow( int i)
ExpandedBlockStart.gif {
InBlock.gif    int total=1;
InBlock.gif    while(i>1)
ExpandedSubBlockStart.gif    {
InBlock.gif        total*=10;
InBlock.gif        i--;
ExpandedSubBlockEnd.gif    }

InBlock.gif    return total;
ExpandedBlockEnd.gif}

None.gif float getNumber( int beginCursor, int endCursor)
ExpandedBlockStart.gif {
InBlock.gif    int pointPosition=1;
InBlock.gif    float number=0;
InBlock.gif    int length,cursor;
InBlock.gif
InBlock.gif    length=endCursor-beginCursor;
InBlock.gif    for(cursor=beginCursor;cursor<endCursor;cursor++)
ExpandedSubBlockStart.gif    {
InBlock.gif        if(isPoint(inputString[cursor]))
ExpandedSubBlockStart.gif        {
InBlock.gif            pointPosition=cursor-beginCursor;
ExpandedSubBlockEnd.gif        }

InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            number=number*10+inputString[cursor]-48;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    return number/getPow(length-pointPosition);
ExpandedBlockEnd.gif}

None.gif void main()
ExpandedBlockStart.gif {
InBlock.gif    int cursor=0,inputLength=0;
InBlock.gif    int rangeBegin=0,rangeEnd=0;
InBlock.gif    float sum=0;
InBlock.gif    gets(inputString);
InBlock.gif
ExpandedSubBlockStart.gif    /* Get the length of the inputString */
InBlock.gif    while(inputString[cursor]!='\0') cursor++;
InBlock.gif    inputLength=cursor;
InBlock.gif
InBlock.gif    cursor=0;
InBlock.gif    while(cursor<inputLength)
ExpandedSubBlockStart.gif    {
InBlock.gif        if(isNumber(inputString[cursor]))
ExpandedSubBlockStart.gif        {
InBlock.gif            rangeBegin=cursor;
InBlock.gif            rangeEnd=getRangeEnd(cursor);
InBlock.gif            sum+=getNumber(rangeBegin,rangeEnd);
InBlock.gif            cursor=rangeEnd+1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            cursor++;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    printf("%f\n",sum);
ExpandedBlockEnd.gif}


评论:感觉这个题目和计算机等级考试的三级题目差不多。应该说还要稍微难一些。我大概用了十多分钟。如果用C#或者Java(ACM允许用Java),加上正则表达式,应该相当容易,呵呵。
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/06/12/423747.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
存储 算法 JavaScript
怎么刷算法,leetcode上有哪些经典题目
怎么刷算法,leetcode上有哪些经典题目
16 0
|
1月前
|
算法
【算法】——动态规划题目讲解
【算法】——动态规划题目讲解
|
7月前
|
存储 算法 决策智能
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(下)
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(下)
48 0
|
7月前
|
算法 C++ 容器
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(上)
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(上)
27 0
|
11天前
|
算法
算法系列--动态规划--背包问题(4)--完全背包拓展题目(上)
算法系列--动态规划--背包问题(4)--完全背包拓展题目(上)
18 0
|
8月前
|
算法 Java
大厂算法题目-单链表删除数字
大厂算法题目-单链表删除数字
大厂算法题目-单链表删除数字
|
4月前
|
算法
class037 二叉树高频题目-下-不含树型dp【算法】
class037 二叉树高频题目-下-不含树型dp【算法】
22 0
class037 二叉树高频题目-下-不含树型dp【算法】
|
4月前
|
算法
class036 二叉树高频题目-上-不含树型dp【算法】
class036 二叉树高频题目-上-不含树型dp【算法】
29 0
|
4月前
|
算法
数据结构字符串匹配KMP算法的详解(题目讲解 简单易懂)
数据结构字符串匹配KMP算法的详解(题目讲解 简单易懂)
32 0
|
6月前
|
机器学习/深度学习 算法 Java
算法宝典2——Java版本(此系列持续更新,这篇文章目前3道)(有题目的跳转链接)(此份宝典包含了二叉树的算法题)
算法宝典2——Java版本(此系列持续更新,这篇文章目前3道)(有题目的跳转链接)(此份宝典包含了二叉树的算法题)

热门文章

最新文章