Hdoj (HDU) 2156 fractional matrix (HM, summation)

Source: Internet
Author: User

Problem Description
We define the following matrices:
1/1 1/2 1/3
1/2 1/1 1/2
1/3 1/2 1/1
The element on the diagonal of the matrix is always 1/1, and the denominator of the scores on both sides of the diagonal increments.
Request the sum of this matrix.

Input
Each row is given an integer n (n<50000), which indicates that the matrix is n*n. When N is 0 o'clock, the input ends.

Output
Output answer, keep 2 decimal places.

Sample Input
1
2
3
4
0

Sample Output
1.00
3.00
5.67
8.83

Simple questions
Do not hit the table will time out ....
You can also use a formula to do, there are rules.

Play table:

Import Java.util.Scanner; Public classmain{Static DoubleDb[] =New Double[50002]; Public Static void Main(string[] args)        {Dabiao (); Scanner sc =NewScanner (System.inch); while(Sc.hasnext ()) {intn =sc.nextint ();if(n==0){return; } System. out. printf ("%.2f", Db[n]); System. out. println (); }    }Private Static void Dabiao() {db[1]=1;Doublem =1; for(intI=2; i<db.length;i++) {m=m+2.0*1.0/I; db[i]=db[i-1]+m; }    }}

Find the pattern:
Analysis:

Initial conditions: a[5005]={0,1,3}1/1a[1]1/1 1/21/2 1/1a[2]1/1 1/2 | 1/31/2 1/1 |--------|1/3 1/2   1/1a[3] ____________|1/1 1/3|____________|1/2 |1/1 1/2| 1/3||1/3 |1/2 1/1| 1/2||____|_______| | 1/4 |1/3 1/2 1/1| A[4]     |____________|

Recursive formula: a[i]=2*a[i-1]-a[i-2]+2.0/i;
Initial conditions: a[5005]={0,1,3}

 #include <stdio.h>  double  a[50005 ]={0 , 1 , 3 }; int  Main () {int  n,i; for  (I=3 ; I<=50000 ; i++) A[i]=2  *a[i-1 ]-a[i-2 ]+2.0 /I; while  (scanf  ( "%d" , &n)!=eof&&n) printf  ( "%.2FN" , A[n]); return  0 ;} 

Hdoj (HDU) 2156 fractional matrix (hm, SUM)

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.