首页 > 代码库 > ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

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

ZOJ 2971 Give Me the Number 题目

ZOJ 2311 Inglish-Number Translator 题目

 

 

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

 

 

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

#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代码(这题要注意,防空行,就是一行什么都没输入,那么也什么都不要输出):

//模拟//英文句子转阿拉伯数字。#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