找规律:
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;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }