Hdoj HDU 1715 maifo ACM 1715 in HDU string add Template

Source: Internet
Author: User

Miyu original, post Please note: Reprinted from __________ White House

Question address:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1715
Description:

Problem description
The Fibonacci series is defined as follows:
F (1) = F (2) = 1
F (n) = f (n-1) + f (n-2) n> = 3.
Calculate the value of the nth term.

Input
Enter the first behavior as an integer N, And the next n behavior as an integer Pi (1 <= pI <= 1000 ).

Output
The output is n rows, and each row corresponds to F (PI ).

Sample Input
5
1
2
3
4
5

Sample output
1
1
2
3
5

When I look at the question, I will know that it is a big number... directly templates. There is nothing to say.
The Code is as follows:

# Include <iostream>
# Include <string>
Using namespace STD;

String add (string fnum, string snum)
{
If (fnum. Length () <snum. Length () fnum. Swap (snum );
String A = "0 ";
A + = fnum;
For (INT I = 1; I <= fnum. Length (); I ++)
If (I <= snum. Length ())
A [A. Length ()-I] + = snum [snum. Length ()-I]-'0 ';
For (INT I = 1; I <A. Length (); I ++)
{
If (A [A. Length ()-I]> '9 ')
{A [A. Length ()-I]-= 10; A [A. Length ()-i-1] + = 1 ;}
}
While (A [0] = '0') A. Erase (0, 1 );
Return;
}

String f [1001] = {"0", "1", "1 "};
Void setnum ()
{
For (INT I = 3; I! = 1001; ++ I)
{
F [I] = add (F [I-1], F [I-2]);
}
}
Int main ()
{
Int T;
Setnum ();
While (CIN> T)
{
While (t --)
{
Int N;
Cin> N;
Cout <F [N] <Endl;
}
}
Return 0;
}

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.