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 }

 

转载于:https://www.cnblogs.com/xxx0624/archive/2012/12/06/2805683.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值