Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

POJ 2006 Litmus Test---简单题

Posted on 2009-11-12 22:38 Uriel 阅读(556) 评论(0)  编辑 收藏 引用 所属分类: POJ
就是个简单化学问题。。
方程是: nx^2+kx-mck=0 (k==ka,c==已知初始浓度) 解下就行
搞了好几个小时。。WA无数次。。很无语。。以为是精度问题。。结果是输入没处理好。。不一定是sample那样的。。
然后改啊改。。结果是90行搞输入和第一步处理。。计算只要3行。。。猥琐丑陋的代码如下:
/*Problem: 2006  User: Uriel 
   Memory: 216K  Time: 125MS 
   Language: C++  Result: Accepted
*/
 

#include
<math.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>

char str1[10],str2[10];
double n,m,s;
double res,tmp,c,k,delta,x;
int flag;

int main()
{
    
int i,j;
    
while(1)
    
{
        scanf(
"%s",str1);
        scanf(
"%s",str2);
        scanf(
"%lf %lf",&m,&n);
        
if(strcmp(str1,"0")==0 && strcmp(str2,"0")==0 && !&& !m)break;
        k
=0;
        s
=1;
        flag
=1;
        
for(i=0;i<strlen(str1);i++)
        
{
            
if(str1[i]=='e')break;
            
if(str1[i]=='.')
            
{
                flag
=2;
                
continue;
            }

            
if(flag==1)
            
{
                k
=k*10+str1[i]-'0';
            }

            
else if(flag==2)
            
{
                s
*=0.1;
                k
+=(str1[i]-'0')*s;
            }

        }

        s
=0;
        flag
=1;
        
for(j=i+1;j<strlen(str1);j++)
        
{
            
if(str1[j]=='-')
            
{
                flag
=2;
            }

            
else 
            
{
                s
=s*10+str1[j]-'0';
            }

        }

        
if(flag==2)s*=-1;
        k
*=pow(10.0,s);     
        c
=0;
        s
=1;
        flag
=1;
        
for(i=0;i<strlen(str2);i++)
        
{
            
if(str2[i]=='e')break;
            
if(str2[i]=='.')
            
{
                flag
=2;
                
continue;
            }

            
if(flag==1)
            
{
                c
=c*10+str2[i]-'0';
            }

            
else if(flag==2)
            
{
                s
*=0.1;
                c
+=(str2[i]-'0')*s;
            }

        }

        s
=0;
        flag
=1;
        
for(j=i+1;j<strlen(str2);j++)
        
{
            
if(str2[j]=='-')
            
{
                flag
=2;
            }

            
else 
            
{
                s
=s*10+str2[j]-'0';
            }

        }

        
if(flag==2)s*=-1;
        c
*=pow(10.0,s); 
        getchar();
        delta
=sqrt(k*k+4*n*c*k*m);
        tmp
=(-k+delta)/(2*n);
        res
=-log10(tmp);
        printf(
"%.3lf\n",res);
    }

    system(
"PAUSE");
    
return 0;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理