Java大数加法 HDU1002 HDU1250

 1 import java.math.*;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main{
 6     public static void main(String[] args){
 7         BigInteger a,b;
 8         int n;
 9         BigInteger ans;
10         Scanner cin = new Scanner(System.in);
11         n=cin.nextInt();
12         a=cin.nextBigInteger();
13         b=cin.nextBigInteger();
14         ans=a.add(b);
15         System.out.println("Case 1:");
16         System.out.println(a+" + "+b+" = "+ans);
17         for(int i=2;i<=n;i++)
18         {
19             a=cin.nextBigInteger();
20             b=cin.nextBigInteger();
21             ans=a.add(b);
22             System.out.println();
23             System.out.println("Case "+i+":");
24             System.out.println(a+" + "+b+" = "+ans);
25         }
26     }
27 }
View Code

 

 1 import java.math.*;
 2 import java.util.*;
 3 
 4 public class Main
 5 {
 6     static BigInteger arr[] = new BigInteger [1000000];
 7     public static void main(String[] args)
 8     {
 9         Scanner cin=new Scanner(System.in);
10         int n;
11         while(cin.hasNext())
12         {
13             n=cin.nextInt();
14             arr[1]=arr[2]=arr[3]=arr[4]=BigInteger.ONE;
15             for(int i=5;i<=n;i++)
16             {
17                 arr[i]=BigInteger.ZERO;    
18                 arr[i]=arr[i].add(arr[i-1]);
19                 arr[i]=arr[i].add(arr[i-2]);
20                 arr[i]=arr[i].add(arr[i-3]);
21                 arr[i]=arr[i].add(arr[i-4]);
22             }
23             System.out.println(arr[n]);
24         }
25     }
26 }
View Code

 

posted @ 2015-08-04 20:08  相儒以沫  阅读(144)  评论(0编辑  收藏  举报