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;
}

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

时间: 2024-08-07 06:13:51

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

2017 ACM/ICPC Asia Regional Qingdao Online 1011 A Cubic number and A Cubic Number

A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A cubic number is the result of using a whole number in a mul

hdu 6216 A Cubic number and A Cubic Number【数学】

hdu 6216 A Cubic number and A Cubic Number 题意:判断一个素数是否是两个立方数之差,就是验差分. 题解:只有相邻两立方数之差才可能,,因为x^3-y^3=(x-y)(x^2+xy+y^2),看(x-y),就能很快想到不相邻的立方数之差是不可能是素数的:),,然后把y=x+1代入,得:p=3x^2+3x+1,所以可得判断条件为:①p-1必须能被3整除:②(p-1)/3必须能表示为两个相邻整数之积. 1 #include<iostream> 2 #incl

[Javascript] Use Number() to convert to Number if possilbe

Use map() and Number() to convert to number if possilbe or NaN. var str = ["1","1.23","3","4.0","five", undefined, .00]; var num = str.map( (s) => { return Number(s); }); console.log(num); //[1, 1.23, 3

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

&#39;Invalid update: invalid number of sections. The number of sections contained in the table view aft

问题:(删除tableview中的section时) Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (5) must be equal to th

HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】

Problem Description A cubic number is the result of using a whole number in a multiplication three times. For example, 3×3×3=27 so 27 is a cubic number. The first few cubic numbers are 1,8,27,64 and 125 . Given an prime number p . Check that if p is

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

184. Largest Number/179. Largest Number 本题难度: Medium Topic: Greedy Description Largest Number 中文English Given a list of non negative integers, arrange them such that they form the largest number. Example Given [1, 20, 23, 4, 8], the largest formed nu

hdu 6216 A Cubic number and A Cubic Number

题意:给定一个素数,判定它是不是两个立方数之差. 题解:对于a^3+b^3=(a-b)(a^2-a*b+b^2),而一个素数的因子只有1和其本身,在加上(a^2-a*b+b^2)一定是大于1的,所以只有(a-b)为1的时候,才可能为解.预处理一下,打个表就好了. ac代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace