poj1001Exponentiation

poj1001Exponentiation
大说乘法运算,Java中有BigDecimal类支持,关键是格式要求。java中应该有专门处理格式的类吧,不过我这里用的是最原始的格式处理方式——字符串拆分。
import  java.io. * ;
import  java.util. * ;
import  java.math. * ;
class  Main
{
    
public static void main(String[] args)
    
{
        
int p;
        BigDecimal dc;
        Scanner sc 
= new Scanner(System.in);
        
while(sc.hasNext())
        
{
            dc 
= sc.nextBigDecimal();
            p 
= sc.nextInt();
            
            dc 
= dc.pow(p);
            String strdc[] 
= dc.toString().split("E-");
            
            StringBuffer strbf 
= new StringBuffer();
            
if(strdc.length == 2)
            
{
                strbf.append(
".");
                
char[] charAry = strdc[0].toString().toCharArray();
                
                
int zeros = new Integer(strdc[1]);
                
                
for(int i = 1; i < zeros; i++)
                
{
                    strbf.append(
"0");
                }

                strbf.append(charAry[
0]);

                
if(!strdc[0].equals("1"))
                
{
                    strbf.append(strdc[
0].toString().toCharArray(), 
                        
2, strdc[0].toString().toCharArray().length - 2);
                }

                
            }

            
else
            
{
                strbf.append(dc);
            }

            
            
int i = strbf.length() - 1;
            
while(strbf.charAt(i) == '0')
                i
--;
            
if(strbf.charAt(i) == '.')
                i
--;
            String rs 
= new String(strbf.toString().toCharArray(), 0, i + 1);
            System.out.println(rs);
        }

    }

}

你可能感兴趣的:(poj1001Exponentiation)