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-02 04:41:36

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

HDOJ(HDU) 2162 Add ‘em(求和)

Problem Description Write a program to determine the summation of several sets of integers. Input The input file will consist of up to 250 sets of integers, where each set contains at most 100 integers and the integer values will be between –16000 an

HDOJ(HDU) 2304 Electrical Outlets(求和、、)

Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating back to the days before people had electricity in their houses. Because of this, Roy's apartment has only one single wall ou

HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)

Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t =sc.nextInt()

HDOJ/HDU 1161 Eddy&amp;#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 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle

HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)

Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合各种ACM算法生成的老鼠药效果奇好,据说该药专对老鼠有效,如果被人误食了,没有任何副作用,甚至有传闻说还有健胃的效果,不过这倒没有得到临床验证.所以,公司的销量逐年递增,利润也是节节攀升,作为股东之一的公主负责财务,最近半年,她实在辛苦,多次因为点钞票造成双手抽筋而住院,现在在她面前你根本不要提到"

HDOJ(HDU) 2107 Founding of HDU(找最大值)

Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等ACM队员集体退役,甚至正在酝酿退学. 为什么?要考研?那也不用退学呀- 当然不是!真正的原因是他们想提前创业,想合伙成立一家公司,据说公司的名称都想好了,为了感谢多年的ACM集训队队长XHD,公司就叫海东集团(HaiDong Union),简称HDU.(对于这个公司名称,几个人私下里开玩笑说,外面的人看到HDU,可别以为是"胡捣集团",呵呵) 公司成立了,谁来做老大呢?这对于合伙的

HDOJ(HDU) 2500 做一个正气的杭电人(水~)

Problem Description 做人要有一身正气,杭电学子都应该如此.比如我们今天的考试就应该做到"诚信"为上. 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的"HDU"字符串,特别地,为了体现"正气"二字,我们要求输出的字符串也是正方形的(行数和列数相等). Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个