Large number of problems, usually in Java

Source: Internet
Author: User

e.g.

HDU1002

import Java.math.biginteger;import Java.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner cin=NewScanner (System.inch); intt=Cin.nextint ();        Cin.nextline (); intCnt=0;  while(t!=0) {T--; String S1=Cin.next (); String S2=Cin.next (); BigInteger A1=NewBigInteger (S1); BigInteger A2=NewBigInteger (S2); System. out. println (" Case"+ (++CNT) +":"); System. out. Print (a1+" + "+a2+" = "); System. out. println (A1.add (A2)); if(t!=0) System. out. println (); }            }}

HDU1715 Large Fibonacci number

ImportJava.math.BigInteger;ImportJava.util.Scanner; Public classMain {Private Staticbiginteger[] fib;  Public Static voidf () {fib=Newbiginteger[1005]; fib[2]=NewBigInteger ("1"); fib[1]=fib[2];  for(inti=3;i<1005;i++) {Fib[i]=fib[i-1].add (fib[i-2]); }    }     Public Static voidMain (string[] args) {Scanner cin=NewScanner (system.in); intt=Cin.nextint ();        Cin.nextline (); intCnt=0;        f ();  while(t!=0) {T--; CNT=Cin.nextint ();        System.out.println (fib[cnt]); }            }}

HDU1865

Analysis (RPM):

simple recursion + large number.
F[n]=f[n-1]+f[n-2].

Why?
since the numbers are only ' 1 ' and ' 2 ', then when you let Nth
When the bit is 1, f[n] plus f[n-1]; When you let the last two bits
Merged into 2, F[n] plus f[n-2] (because to ensure
The penultimate digit must be 1).

The calculated F is a Fibonacci sequence.

WOW, problem transformation is extremely ingenious.

The serious point of this recursive relationship is dynamic planning.

ImportJava.math.BigInteger;ImportJava.util.Scanner; Public classMain {Private Staticbiginteger[] fib;  Public Static voidf () {fib=Newbiginteger[1005]; fib[1]=NewBigInteger ("1"); fib[0]=fib[1];  for(inti=2;i<1005;i++) {Fib[i]=fib[i-1].add (fib[i-2]); }    }     Public Static voidMain (string[] args) {Scanner cin=NewScanner (system.in); intt=Cin.nextint ();        Cin.nextline (); intCnt=0;        f ();  while(t!=0) {T--; String s=Cin.next (); CNT=s.length ();        System.out.println (fib[cnt]); }            }}

Large number of problems, usually in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.