hdu1228双指针

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char s[100],*dic[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int A[5],B[5],ANS_A,ANS_B,ca,cb;
bool compare(char *s,char *p){
    if(strlen(s)!=strlen(p)) return false;
    int i;for(i=0;i<strlen(s);++i) if(s[i]!=p[i]) return false;return true;
}
int convert(char* s){
    int i;for(i=0;i<10;++i) if(compare(s,dic[i])) return i;
}
void convA_B(){
    int i;ANS_A=0,ANS_B=0;//全局变量重新声明导致未初始化
    for(i=0;i<ca;++i)    ANS_A=ANS_A*10+A[i];
    for(i=0;i<cb;++i)    ANS_B=ANS_B*10+B[i];
}
int main(){
    char c;int cnt=0,i,j,END=0;
    while(!END){
        c=getchar();
        if(c!='\n') s[cnt++]=c;
        else{
            s[cnt++]=' ';s[cnt++]='\0';
            int p1=-1,p2=-1,flag=0;
            ca=0;cb=0;
            for(i=0;i<cnt-1;++i){
                if(s[i]!=' '){if(p1==-1)p1=p2=i;else p2=i;}
                else {
                    if(p1!=-1){
                        if(p1==p2){if(s[p1]=='+') flag=1;}
                        else{
                            char str[10];int cc=0;
                            for(j=p1;j<=p2;++j) str[cc++]=s[j];str[cc]='\0';
                            if(!flag) A[ca++]=convert(str);
                            else      B[cb++]=convert(str);
                        }
                        p1=p2=-1;
                    }
                }
            }
            cnt=0;convA_B();
            if(ANS_A==0&&ANS_B==0) {END=1;break;}printf("%d\n",ANS_A+ANS_B);
        }
    }
    return 0;
}

 

posted @ 2017-03-07 10:34  狡啮之仰  阅读(181)  评论(0编辑  收藏  举报