The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number

来源:互联网 发布:centos 7开机密码忘记 编辑:程序博客网 时间:2024/06/05 19:23

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971

题意:将输入的英文数字表达转化为阿拉伯数字。

#include<bits/stdc++.h>using namespace std;char aa[30][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"                 };int bb[28]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};char cc[300][20];int main() {    int t;    char s[250];    scanf("%d",&t);    getchar();//先吃掉t后面的回车    while(t--) {        memset(s,0,sizeof(s));        memset(cc,0,sizeof(cc));        gets(s);        int num=0,k=0;        int len=strlen(s);        for(int i=0; i<len; i++) {            if(i>=1&&s[i]==' '&&s[i-1]!=' ') {                num++;//在’ ‘前算上一个字符串                k=0;                continue;            }            if(s[i]!=' ')                cc[num][k++]=s[i];//装入一个字符串        }        int sum1=0,sum2=0;        int j;        //有前至后读入字符串        for(int i=0; i<=num; i++) {            for(j=0; j<28; j++) {                if(strcmp(cc[i],aa[j])==0) {                    sum1+=bb[j];                    break;                }            }            if(j=28) {                if(strcmp(cc[i],"and")==0)                    continue;                if(strcmp(cc[i],"thousand")==0) {                    sum1*=1000;                    sum2+=sum1;                    sum1=0;                } else if(strcmp(cc[i],"hundred")==0)                    sum1*=100;                else if(strcmp(cc[i],"million")==0) {                    sum1*=1000000;                    sum2+=sum1;                    sum1=0;                }            }        }        printf("%d\n",sum2+sum1);    }    return 0;}//善于使用strcmp函数,其实想到就很简单了
0 0