码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

时间:2014-06-27 17:11:36      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

ZOJ 2971 Give Me the Number 题目

ZOJ 2311 Inglish-Number Translator 题目

 

 

//两者题目差不多,细节有点点不一样,因为不是一起做的,所以处理方式有一点点不一样——还是前一个方式高端呀。

 

 

//ZOJ 2971 的AC代码(用了strstr函数):

bubuko.com,布布扣
#include<stdio.h>
#include<string.h>
char c[33][10] = {"zero" , "one" , "two" , "three" , "four" , "five" , "six"  , "seven" , "eight" , "nine", "ten" ,
    "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen", "twenty" ,
    "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" , "hundred" , "thousand" , "million" , "and"};
char s[200],sss[200];
int main()
{
    int ne , sum , flag , i,j , acc,len;
    int tt;
    scanf("%d",&tt);
    getchar();
    while(tt--)
    {
        gets(s); 
        ne = 1;
        sum = 0;
        flag = 0;
        acc = 0;
        len=strlen(s);
        while (flag <len) 
        {
            while (s[flag] ==  ) flag++;
            //    while (s[flag] == ‘and‘) flag++;
            for (i = 31 ; i >= 0 ; i--)
            {
                if (strstr(&s[flag] , c[i]) == &s[flag])//在s中找c[i]如果找到了,返回位置,没有找到返回-1,即false;&取位置
                {
                    if (i <= 20) acc += i;
                    else if (i <= 27) acc += (i - 18) * 10;
                    else if (i == 28) acc *= 100;
                    else if (i == 29)  
                    {
                        sum += acc * 1000;
                        acc = 0;
                    }
                    else if (i == 30) 
                    {
                        sum += acc * 1000000;
                        acc = 0;
                    }
                    else if (i == 31) ne = -1;
                    flag += strlen(c[i]);
                    break;
                }
            }
        }
        printf("%d\n" , (sum + acc));

    }
    return 0;
}
字符串转化为数

 

 

//ZOJ 2311 的AC代码(这题要注意,防空行,就是一行什么都没输入,那么也什么都不要输出):

bubuko.com,布布扣
//模拟
//英文句子转阿拉伯数字。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{
    int len,i,j,k,num,sign,ans;
    char str[1010],word[20];
    char w[33][20]={"negative", "zero", "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
        "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
        "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
        "ninety", "hundred", "thousand", "million"};
    while(gets(str))
    {
        if(strcmp(str,"")==0)continue;//防空行
        len=strlen(str);
        str[len++]= ;
        num=0;
        j=0;
        sign=1;
        ans=0;
        for(i=0;i<len;i++)
        {
            if(str[i]!= )
                word[j++]=str[i];
            else 
            {
                word[j++]=\0;
                for(k=0;k<32;k++)
                {
                    if(strcmp(w[k],word)==0)
                    {
                        if(k==0)
                            sign=-1;
                        else if(k>=1&&k<=21)
                            num+=k-1;
                        else if(k>=22&&k<=28)
                            num+=(k-19)*10;
                        else if(k==29)
                            num=num*100;
                        else if(k==30)//只要考虑1000
                        {
                            ans+=num*1000;
                            num=0;
                        }
                        else if(k==31)//和1000000就可以了
                        {
                            ans+=num*1000000;
                            num=0;
                        }
                        break;
                    }
                }
                j=0;
            }
        }
        ans+=num;
        printf("%d\n",ans*sign);
    }
    return 0;
}
View Code

 

 

 

ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr),布布扣,bubuko.com

ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/laiba2004/p/3810039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!