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)