The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

福大月赛 B题

这题说白了 其实很简单,就是求一个大数乘以 (根号二+1)
但奇怪的是我居然PE了,郁闷....求解答
下面是我的代码:

import java.io.*;
import java.math.
*;
import java.util.
*;

public class Main 
{
    
    
public static void main(String[] args) 
    
{
        String res;
        BigDecimal a
=new BigDecimal("0");
        BigDecimal b
=new BigDecimal("0");
        BigDecimal c
=new BigDecimal("0");
        
int tt=0;
        
        Scanner cin 
= new Scanner (new BufferedInputStream(System.in));
        
while(cin.hasNext())
        
{
            tt
++;
    
                
            System.
out.printf("Case %d\n",tt);
            
double t;
            a
=cin.nextBigDecimal();
            b
=BigDecimal.valueOf( (Math.sqrt(2)+1 ) );
            c
=BigDecimal.valueOf(100000.0);
            a
=a.multiply(b);
            
int i;
            
if(a.compareTo(c)==1||a.compareTo(c)==0)
            
{
                res
=a.toPlainString();

                
for(i=0;i<5;i++)
                    System.
out.printf("%c",res.charAt(i));
                System.
out.println();
            }

            
else
            
{
                res
=a.toPlainString();
                i
=0;
                
while(res.charAt(i)!='.')
                
{
                    
                    System.
out.printf("%c",res.charAt(i));
                    i
++;
                }

                System.
out.printf(".");
                i
++;
                
int j=i;
                
if(res.charAt(j+4)<'5')
                
{
                    
int k;
                    
for(k=i;k<i+4;k++)
                        System.
out.printf("%c", res.charAt(k));
                }

                
                
else
                
{
                    
int k;
                    
for(k=i;k<i+3;k++)
                        System.
out.printf("%c", res.charAt(k));
                    System.
out.printf("%c", res.charAt(k)+1);
                }

                
                System.
out.println();
            }

            System.
out.println();
            
            
        }

        
    }


}

终于AC了,还顺便把DecimalFormat学习了一下,代码如下:
原来java里面的换行符不是'\n'啊,把PE时候的那一行 System.out.printf("Case %d\n",tt);改成System.out.println("Case "+tt);就对了。。。汗 我太相信C语言了。。
RunID: 265601
UserID: abilitytao
Submit time: 
2010-02-08 11:37:16
Language: JavaLength: 
1317 Bytes.
Result: Accepted
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public class Main 
{
    
    
public static void main(String[] args) 
    
{
        String res;
        BigDecimal a
=new BigDecimal("0");
        BigDecimal b
=new BigDecimal("0");
        BigDecimal c
=new BigDecimal("0");
        
int tt=0;
        
        Scanner cin 
= new Scanner (new BufferedInputStream(System.in));
        
while(cin.hasNext())
        
{
            tt
++;
    
                
            System.out.println(
"Case "+tt);
            
double t;
            a
=cin.nextBigDecimal();
            b
=BigDecimal.valueOf( (Math.sqrt(2)+1 ) );
            c
=BigDecimal.valueOf(100000.0);
            a
=a.multiply(b);
            
int i;
            
if(a.compareTo(c)==1||a.compareTo(c)==0)
            
{
                res
=a.toPlainString();

                
for(i=0;i<5;i++)
                    System.out.print(res.charAt(i));
                System.out.println();
            }

            
else
            
{
                DecimalFormat my
=new DecimalFormat("#.0000");
                String ans
=my.format(a);
                System.out.println(ans);
                
            }

            System.out.println();
            
            
        }

        
    }


}

posted on 2009-11-28 17:32 abilitytao 阅读(1149) 评论(0)  编辑 收藏 引用


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