HDU1041

找规律:

n=1 ans=0;

n=2 ans=1;

n=3 ans=1;

.....

if( n%2==1 ) ans=ans*2-1;

else ans=ans*2+1;

View Code
 1 import java.util.Scanner;
 2 import java.math.*;
 3 //import java.text.*;
 4 //import java.lang.*;
 5 public class b{
 6     public static void main( String srga[] ){
 7         Scanner cin=new Scanner( System.in );
 8         while( cin.hasNext() ){
 9             int n;
10             n=cin.nextInt();
11             BigInteger ans=BigInteger.valueOf(1);
12             BigInteger tmp1=BigInteger.valueOf( 1 );
13             BigInteger tmp2=BigInteger.valueOf( 2 );
14             BigInteger tmp3=BigInteger.valueOf( 0 );
15             if( n==1 ) {
16                 System.out.println( tmp3 );
17                 continue;
18             }
19             if( n==2 ) {
20                 System.out.println( tmp1 );
21                 continue;
22             }
23             if( n==3 ) {
24                 System.out.println( tmp1 );
25                 continue;
26             }
27             for( int i=4;i<=n;i++ ){
28                 if( i%2==1 ){
29                     ans=ans.multiply( tmp2 );
30                     ans=ans.subtract( tmp1 );
31                 }
32                 else{
33                     ans=ans.multiply( tmp2 );
34                     ans=ans.add( tmp1 );
35                 }
36             }
37             System.out.println( ans );
38         }
39     }
40 }

 

posted @ 2012-12-06 19:13  xxx0624  阅读(440)  评论(0编辑  收藏  举报