天天看點

uva11809

送出wa,還需改進,應該是浮點數精度問題

#include
   
    
#include
    
     
#include
     
      
double bin_to_dig(int m,int e){
    double a,b;
    a = 1-pow(2,-1*(m+1));
    b = pow(2,e)-1;
    return a*pow(2,b);
}
double char_to_dou(char* str){
    int i=1;
    double zhishu=1;
    double sum=0;
    while(str[++i]!='e'){

        zhishu*=0.1;
        sum+=zhishu*(str[i]-'0');
    }
    sum+=str[0]-'0';
    int q=0;
    while(str[++i]!='\0'){
        q=q*10+str[i]-'0';
    }
    return sum*pow(10,q);;

}
//考慮浮點數計算誤差的問題
int main(){
   char str[50];
   double f,g;
   int m,e;
   while(scanf("%s",str)&&strcmp(str,"0e0")){
        f = char_to_dou(str);
        int fg=0;
        for(m=0;m<10;m++){
            for(e=1;e<31;e++){
                g = bin_to_dig(m,e);
                if(g==f){
                    fg=1;
                    break;
                }
            }
            if(fg)break;
        }
        if(fg) printf("%d %d\n",m,e);
   }

    return 0;
}