九度OJ 1386(最值) 1387(递归) 1388(递归) 1389(递归) 1390(递归)

来源:互联网 发布:centos yum安装svn 编辑:程序博客网 时间:2024/06/05 06:09

1386:旋转数组的最小数字

http://ac.jobdu.com/problem.php?pid=1386

题意

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。

思路

这个题不就是求数组最小值吗?跟旋转有什么关系?

代码

#include <stdio.h>#define N 1000000int main(){    int i,n;    int a[N];    int result;    while(scanf("%d", &n) != EOF)    {        for (i=0; i<n; i++)            scanf("%d", &a[i]);        if (n == 1)        {            printf("%d\n", a[0]);            continue;        }        for (i=1; i<n; i++)        {            if (a[i] < a[i-1])                break;        }        if (i==n)            result = a[0];        else            result = a[i];        printf("%d\n", result);    }    return 0;}/**************************************************************    Problem: 1386    User: liangrx06    Language: C    Result: Accepted    Time:700 ms    Memory:4748 kb****************************************************************/

1387:斐波那契数列

http://ac.jobdu.com/problem.php?pid=1387

题意

求第n项斐波那契数列的值。

思路

用数组递归。

代码

#include <stdio.h>int main(void){    int n, i;    long long a[71];    while (scanf("%d", &n) != EOF)    {        a[0] = 0;        a[1] = 1;        for (i=2; i<=n; i++)            a[i] = a[i-1] + a[i-2];        printf("%lld\n", a[n]);    }    return 0;}/**************************************************************    Problem: 1387    User: liangrx06    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/

1388:跳台阶

http://ac.jobdu.com/problem.php?pid=1388

题意

实际上是求斐波那契数列。

思路

数组递归。

代码

#include <stdio.h>int main(){    int i,n;    long long a[71];    while(scanf("%d", &n) != EOF)    {        a[0] = a[1] = 1;        for (i=2; i<=n; i++)            a[i] = a[i-1] + a[i-2];        printf("%lld\n", a[n]);    }    return 0;}/**************************************************************    Problem: 1388    User: liangrx06    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/

1389:变态跳台阶

http://ac.jobdu.com/problem.php?pid=1389

题意

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

思路

已经不是斐波那契数列,但仍然用数组递归来求。

代码

#include <stdio.h>int main(){    int i, j, n;    long long a[71];    while(scanf("%d", &n) != EOF)    {        a[0] = a[1] = 1;        for (i=2; i<=n; i++)        {            j = i-1;            a[i] = 0;            while (j>=0)            {                a[i] += a[j];                j--;            }        }        printf("%lld\n", a[n]);    }    return 0;}/**************************************************************    Problem: 1389    User: liangrx06    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/

1390:矩形覆盖

http://ac.jobdu.com/problem.php?pid=1390

题意

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

思路

实际上是斐波那契数列。

代码

#include <stdio.h>int main(){    int i,n;    long long a[71];    while(scanf("%d", &n) != EOF)    {        a[0] = a[1] = 1;        for (i=2; i<=n; i++)            a[i] = a[i-1] + a[i-2];        printf("%lld\n", a[n]);    }    return 0;}/**************************************************************    Problem: 1390    User: liangrx06    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/
0 0
原创粉丝点击