HDOJ(HDU) 2156 分数矩阵(嗯、求和)

Problem Description

我们定义如下矩阵:

1/1 1/2 1/3

1/2 1/1 1/2

1/3 1/2 1/1

矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增。

请求出这个矩阵的总和。

Input

每行给定整数N (N<50000),表示矩阵为 N*N.当N为0时,输入结束。

Output

输出答案,保留2位小数。

Sample Input

1

2

3

4

0

Sample Output

1.00

3.00

5.67

8.83

简单题

不打表会超时。。。。

还可以用一个公式做,有规律。

打表:

import java.util.Scanner;

public class Main{
    static double db[] = new double[50002];
    public static void main(String[] args) {
        dabiao();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n =sc.nextInt();
            if(n==0){
                return;
            }
            System.out.printf("%.2f",db[n]);
            System.out.println();
        }
    }
    private static void dabiao() {
        db[1]=1;
        double m =1;
        for(int i=2;i<db.length;i++){
            m=m+2.0*1.0/i;
            db[i]=db[i-1]+m;
        }
    }
}

找规律:

分析:

初始条件:a[5005]={0,1,3}

1/1  a[1]

1/1 1/2
1/2 1/1   a[2]

1/1 1/2 | 1/3
1/2 1/1 | 1/2
--------|
1/3 1/2   1/1   a[3]

 ____________
|1/1  1/2 1/3| 1/4
      ____________
|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]
     |____________|

递推公式:a[i]=2*a[i-1]-a[i-2]+2.0/i;

初始条件: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;
}

时间: 2024-10-05 02:11:46

HDOJ(HDU) 2156 分数矩阵(嗯、求和)的相关文章

HDU 2156 分数矩阵

分数矩阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4602 Accepted Submission(s): 2785 Problem Description 我们定义如下矩阵: 1/1 1/2 1/3 1/2 1/1 1/2 1/3 1/2 1/1 矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增. 请求出这个矩阵的总和.

HDUJ 2156 分数矩阵

分数矩阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4305    Accepted Submission(s): 2593 Problem Description 我们定义如下矩阵: 1/1 1/2 1/3 1/2 1/1 1/2 1/3 1/2 1/1 矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增. 请求出这个

HDOJ 2156 分数矩阵

分数矩阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4580    Accepted Submission(s): 2769 Problem Description 我们定义如下矩阵:1/1 1/2 1/31/2 1/1 1/21/3 1/2 1/1矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增.请求出这个矩阵的总和.

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

HDU 2604 Queuing 矩阵高速幂

QueuingTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2483    Accepted Submission(s): 1169 Problem Description Queues and Priority Queues are data structures which are known to most computer s

Hdu 4965(矩阵快速幂)

题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 87    Accepted Submission(s): 39 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a

HDOJ/HDU 1161 Eddy&#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说