A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f(1)=1,f(2)=1,f(n>2)=f(n−1)+f(n−2)f(1) = 1, f(2) = 1, f(n > 2) = f(n − 1) + f(n − 2)f(1)=1,f(2)=1,f(n>2)=f(n−1)+f(n−2)
    Little Tuhin is playing with Fibonacci numbers. He took a paper and began to write non-negative integers such that every nth line contains a total of f(n) numbers. He noted the median of the lines.
    He came to me with a confused smile. He told me, “I think I have found a formula to find the median of a certain line, but I am not sure about my formula. Can you write a code for me? I will give a line number and you have to tell me the median of that line.”
His paper is noted below:
0 Median: 0
1 Median: 1
2 3 Median: 2
4 5 6 Median: 5
7 8 9 10 11 Median: 9
12 13 14 15 16 17 18 19 Median: 15
. . .
    If there are n numbers in a certain line then
Median={(n+1)/2-th Element, n oddn/2-th Element, n evenMedian = \left \{ \begin{array}{lr} (n + 1)/2 &\text{-th Element, n odd}\\ n/2 & \text{-th Element, n even} \end{array} \right. Median={(n+1)/2n/2​-th Element, n odd-th Element, n even​
    My computer is not working well. So, I cannot help him right now. Can anyone help me with the code?
Input
The input file contains several sets of inputs. The total number of sets will be less than 100. The description of each set is given below:
    Each set starts with one integer n (1 ≤ n ≤ 1500) which indicates the line number.
    The input will be terminated by the set where n = 0. And this set should not be processed.
Output
For each set in the input, you should first print the set number starting from 1. And the next line should be the median of the nth line. You can assume that the output will fit into 350 digits.
    See the sample input-output for more details. Output should be formatted like the sample output.
Sample Input
1
2
3
4
5
6
0
Sample Output
Set 1:
0
Set 2:
1
Set 3:
2
Set 4:
5
Set 5:
9
Set 6:
15

问题链接:UVA11161 Help My Brother (II)
问题简述:(略)
问题分析:大数递推问题,用Java来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的Java语言程序如下:

/* UVA11161 Help My Brother (II) */import java.util.Scanner;
import java.math.BigInteger;public class Main {public static void main(String args[]){Scanner input = new Scanner(System.in);int N = 1500 + 1;int caseno = 0;BigInteger f[] = new BigInteger[N + 1];f[1] = BigInteger.ZERO;f[2] = BigInteger.ONE;for(int i = 3; i <= N; i++)f[i] = f[i - 2].add(f[i - 1]).add(BigInteger.ONE);int n = input.nextInt();while (n != 0) {BigInteger median = f[n].add(f[n+1]).subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));System.out.printf("Set %d:\n", ++caseno);System.out.println(median);n = input.nextInt();}}
}

UVA11161 Help My Brother (II)【大数+递推】相关推荐

  1. UVA10359 Tiling【大数+递推】

    In how many ways can you tile a 2 × n rectangle by 2 × 1 or 2 × 2 tiles? Here is a sample tiling of ...

  2. POJ 1737 Connected Graph (大数+递推)

    题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...

  3. HDU 1207 汉诺塔II (递推)

    经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...

  4. 【递推】HDU1207汉诺塔II 【汉诺塔及汉诺塔变形 归纳】

    汉诺塔问题 设f(n)为移动n层的汉诺塔的解,则整个过程其实分为三步: 把n-1层移到第二个上面去(花费f(n-1)) 把最大的移到第三个柱子上面去(花费1) 把n-1层移动到第三个柱子上去(花费f( ...

  5. UVA10862 - Connect the Cable Wires(递推 + java的大数)

    UVA10862 - Connect the Cable Wires(递推 + java的大数) 题目链接 题目大意:给你n座房子位于一条直线上,然后仅仅给你一个cable service.要求每座房 ...

  6. HDU-4477 Cut the rope II 递推

    题意:转化后为给定一个数L,问L拆成N个不同的数相加的方案数,其中N>=2. 解法:其实和上次做的分解的2的幂的数量相同,由于要求每个数都不相同,这里有一个非常好的观察角度,那就是观察一个数的分 ...

  7. 《程序设计基础II》实验3——递推

    A - 养兔子 Description 一对成熟的兔子每天能且只能产下一对小兔子,每次都生一公一母,每只小兔子的成熟期是1天,小兔子出生后隔一天才能再生小兔子.第一天某人领养了一对成熟的兔子,一公一母 ...

  8. OJ《程序设计基础II》实验3——递推

    3-1 A - 养兔子 #include<stdio.h> int main() {int n,i;scanf("%d",&n);long long a[111 ...

  9. 递推--Fibonacci数列 II

    描述 众所周知,Fibonacci数列是一个著名数列.它的定义是: 本题要求采用第二种方法:递推. 输入描述 每行一个整数 i ,表示 Fibonacci 数列的第i项. i ≤ 100000 对比前 ...

最新文章

  1. 区块链概念:Hash 算法
  2. 怎样成为一个高手观后感
  3. 5菜鸟教程_XPLANE10菜鸟基础教程系列 飞机、机场以及天气的设置
  4. 力扣——有序链表转换二叉搜索树
  5. jq实现点击导航栏中的任意一个跳转后被点击的定位到第一个
  6. [WPF]WPF开发方法论
  7. Django(part31)--admin后台数据库管理
  8. 查看修改MySQL字符集
  9. 价格厚道!855最强机皇发布:看完心动了吗?
  10. Linux内存管理:CMA(连续内存分配)(DMA)
  11. php+mysql实例注入,PHP+MYSQL注入实例与防范措施总结
  12. Hadoop之HDFS(二)HDFS基本原理
  13. 决策树系列(二)——剪枝
  14. Visual Studio中实用工具VAssistX
  15. 为Eclipse安装ADT插件
  16. AVX AVX2 To enable them in other operations
  17. U盘格式化后容量变小恢复方法
  18. 入门学习必收藏!精选Photoshop、D…
  19. 在软件开发的早期阶段为什么要进行可行性研究?应该从哪些方面研究目标系统的可行性?
  20. CAS TGT 校验不成功:No principal was found in the response from the CAS server.WHO: audit:unknown

热门文章

  1. 共性的缺失——由博客想到的
  2. python 柱状图设置样式_python数据可视化之图表样式调整(三)
  3. 2.2 The Environment Abstraction Layer (EAL)
  4. 修改JEECG项目浏览器标题
  5. Apache MiNa 实现多人聊天室
  6. 小程序报错:Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail req..
  7. crtmpserver 配置说明_crtmpserver框架代码详解
  8. 1025. 除数博弈
  9. ssh-copy-id命令的介绍,使免密更方便
  10. redis和zookeeper安装教程并配置开机自启