标签:
题目链接:http://poj.org/problem?id=2121
差一点就WA哭了,主要是自己傻逼了。
思路:
遇到hundred,sum*100;
但是遇到thouthend,million,ans+=sum*(... ...),sum=0;
因为到了thouthend,million,后面肯定又是新的数字,跟前面的没关系了,sum=0;
而hundred,后面是可以再加数的,比如22,所以这里只要sum*=100;
注意:
这里经典的一招,杰哥教我的,要重置一下order,只需要order[k]=‘/0‘;
而不需要memset了。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; char str[200]; int main() { while(gets(str)) { //printf("%s",str); if(strcmp(str,"")==0) break; bool flag = false; int ans=0; int sum=0; char order[30]={‘\0‘}; int k=0; for(int i=0;i<strlen(str)+1;i++) { if(str[i]!=‘ ‘&&str[i]!=‘\0‘) order[k++] = str[i]; else{ order[k] = ‘\0‘; k=0; if(!strcmp(order,"negative")) flag = true; if(!strcmp(order,"zero")) sum+=0; if(!strcmp(order,"one")) sum+=1; if(!strcmp(order,"two")) sum+=2; if(!strcmp(order,"three")) sum+=3; if(!strcmp(order,"four")) sum+=4; if(!strcmp(order,"five")) sum+=5; if(!strcmp(order,"six")) sum+=6; if(!strcmp(order,"seven")) sum+=7; if(!strcmp(order,"eight")) sum+=8; if(!strcmp(order,"nine")) sum+=9; if(!strcmp(order,"ten")) sum+=10; if(!strcmp(order,"eleven")) sum+=11; if(!strcmp(order,"twelve")) sum+=12; if(!strcmp(order,"thirteen")) sum+=13; if(!strcmp(order,"fourteen")) sum+=14; if(!strcmp(order,"fifteen")) sum+=15; if(!strcmp(order,"sixteen")) sum+=16; if(!strcmp(order,"seventeen")) sum+=17; if(!strcmp(order,"eighteen")) sum+=18; if(!strcmp(order,"nineteen")) sum+=19; if(!strcmp(order,"twenty")) sum+=20; if(!strcmp(order,"thirty")) sum+=30; if(!strcmp(order,"forty")) sum+=40; if(!strcmp(order,"fifty")) sum+=50; if(!strcmp(order,"sixty")) sum+=60; if(!strcmp(order,"seventy")) sum+=70; if(!strcmp(order,"eighty")) sum+=80; if(!strcmp(order,"ninety")) sum+=90; if(!strcmp(order,"hundred")) { sum=sum*100; } if(!strcmp(order,"thousand")) { ans = ans+sum * 1000; sum = 0; } if(!strcmp(order,"million")) { ans =ans+sum *1000000; sum = 0; } } } if(flag) printf("-%d\n",ans+sum); else printf("%d\n",ans+sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TreeDream/p/5699785.html