递归 2016.3.8

来源:互联网 发布:网络运营商区别 编辑:程序博客网 时间:2024/06/09 16:19

1、HDU 2018 母牛的故事

#include <iostream>using namespace std;int Count(int n){    return (n>0 && n<5) ? n : (Count(n-1)+Count(n-3));}int main(){    int n;    while (cin>>n && n!=0) {        cout<<Count(n)<<endl;    }    return 0;}
#include <iostream>using namespace std;int Count(int low, int high, int y1, int y2, int y3, int y4){    int _1 = y1, _2 = y2, _3 = y3;    if (low >= high) {        return (y1+y2+y3+y4);    }    y4 += _3; y1 = y4; y2 = _1; y3 = _2;//    cout<<y1<<" "<<y2<<" "<<y3<<" "<<y4<<endl;    ++low;    Count(low, high, y1, y2, y3, y4);}int main(){    int n;    int sum;    int y1 = 0, y2 = 0, y3 = 0, y4 = 1;    while (cin>>n && n!=0) {        sum = Count(1, n, y1, y2, y3, y4);        cout<<sum<<endl;    }    return 0;}
#include <iostream>#include <stdio.h>using namespace std;int main(){    int n;    int d = 1,a = 0,b = 0,c = 0,d_save = 1,a_save = 0,b_save = 0,c_save = 0;    int sum = 0;    int i;    while(scanf("%d",&n) != EOF){        if(n){            for(sum=0,d=1,a=0,b=0,c=0,d_save=1,a_save=0,b_save=0,c_save=0,i=2; i<=n; i++){                d = d_save+c_save;                a = d;                b = a_save;                c = b_save;                a_save = a;                b_save = b;                c_save = c;                d_save = d;            }            sum = d+a+b+c;            cout<<sum<<endl;        }    }    return 0;}

2、HDU 1019 Least Common Multiple(最小公倍数)

Problem Description
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.

Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 … nm where m is the number of integers in the set and n1 … nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.

Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.

Sample Input

2
3 5 7 15
6 4 10296 936 1287 792 1

Sample Output

105
10296

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int num[200000];int Gcd(int a, int b){    return (a%b==0 ? b : Gcd(b, a%b));}int main(){//    freopen("in.txt", "r", stdin);    int T;    while (cin>>T) {        while (T--) {            int n;            cin>>n;            int Lcm = 0;            for (int i=0; i<n; ++i) {                cin>>num[i];                if (i == 0) {                    Lcm = num[i];                } else {                    Lcm = Lcm / Gcd(num[i], Lcm) * num[i];                }            }            cout<<Lcm<<endl;        }    }    return 0;}

3、NOJ 1007 母牛生小牛
Description:
设有一头小母牛,从出生第四年起每年生一头小母牛,按此规律,第N年时有几头母牛?
Input:
本题有多组数据。每组数据只有一个整数N,独占一行。( 1 ≤ N ≤ 50 )。当N为0时,输入结束。
Output:
对每组数据,输出一个整数(独占一行)表示第N年时母牛的数量。
Example Input:

1
4
5
9
20
0

Example Output:

1
2
3
13
872

Result : TLE
#include <iostream>#include <cstdio>using namespace std;int Count(int n){    return (n>0 && n<4) ? 1 : (Count(n-1)+Count(n-3));}int main(){    int n;    while (scanf("%d", &n) != EOF) {        printf("%d\n", Count(n));    }    return 0;}
Result : WA
#include <iostream>using namespace std;int Count(int low, int high, int y1, int y2, int y3, int y4){    int _1 = y1, _2 = y2, _3 = y3;    if (low >= high) {        return (y1+y2+y3+y4);    }    y4 += _3; y1 = y4; y2 = _1; y3 = _2;//    cout<<y1<<" "<<y2<<" "<<y3<<" "<<y4<<endl;    ++low;    Count(low, high, y1, y2, y3, y4);}int main(){    int n;    int sum;    int y1 = 1, y2 = 0, y3 = 0, y4 = 0;    while (cin>>n && n!=0) {        sum = Count(1, n, y1, y2, y3, y4);        cout<<sum<<endl;    }    return 0;}
#include <iostream>using namespace std;int y1, y2, y3, y4;void Count(int low, int high){    int _1 = y1, _2 = y2, _3 = y3;    if (low >= high) {        return;    }    y4 += _3; y1 = y4; y2 = _1; y3 = _2;//    cout<<y1<<" "<<y2<<" "<<y3<<" "<<y4<<endl;    ++low;    Count(low, high);}int main(){    int n;    while (cin>>n && n!=0) {        y1 = 1; y2 = 0; y3 = 0; y4 = 0;        Count(1, n);        int sum = y1 + y2 + y3 + y4;        cout<<sum<<endl;    }    return 0;}
#include <iostream>using namespace std;int main(){    int N;    while (cin>>N && N!=0) {        int y4 = 1, y3 = 0, y2 = 0, y1 = 0;        int _4, _3, _2, _1;        N -= 3;        while (N > 0) {            _4 = y4; _3 = y3; _2 = y2; _1 = y1;            y4 += _3;            y3 = _2;            y2 = _1;            y1 = _4 + _3;            --N;        }        cout<<y1+y2+y3+y4<<endl;    }    return 0;}

4、NOJ 2144 汉诺塔++
Description
笨笨的“女神”voidspiral 又被难住了,在 C 语言程序设计课上听不懂老师讲 的“汉诺塔”问题,没办法,只能厚着脸皮去找“sunshine boy”whojay,whojay 开心的表示“女神”又来找他了,作为计算机系的程序猿怎么能没有一点基本的 数学功底呢!我可是要成为青年计算机理论科学家的男人。

不过“女神”给出的规则跟课本上的不一样(当然是为了考验 whojay 嘛)。 就是对于汉诺塔的每次移动只能在相邻的两个柱子上完成,而不能跳跃。 比如:A B C 三个柱子。 对于 A->B 是合法的,而 A->C 是不合法的。whojay 表示 被难住了,可大话已经说出去了,只能希望计算机系的同学来写一个程序来帮助 他完成“女神”的这个任务。

Input

输入有多组测试数据
每组测试数据仅一行,包含一个正整数 N(1<=N<=10)

Output

每组测试第一行“Case T:”
之后每行一个操作“X->Y”
每组输出后一个空行

Sample Input

1
2

Sample Output

Case 1:
A->B
B->C

Case 2:
A->B
B->C
A->B
C->B
B->A
B->C
A->B
B->C

#include <iostream>#include <cstdio>using namespace std;void print_path(int n ,char a, char b, char c){    if (n <= 1) {        printf("%c->%c\n", a, b);        printf("%c->%c\n", b, c);        return;    } else {        print_path(n-1, a, b, c);        printf("%c->%c\n", a, b);        print_path(n-1, c, b, a);        printf("%c->%c\n", b, c);        print_path(n-1, a, b, c);    }}int main(){    int Case = 0;    int N;    while (cin>>N) {        char x = 'A', y = 'B', z = 'C';        ++Case;        cout<<"Case "<<Case<<":"<<endl;        print_path(N, x, y, z);        cout<<endl;    }    return 0;}

5、HDU 2031 进制转换
Problem Description
输入一个十进制数N,将它转换成R进制数输出。

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

Sample Input

7 2
23 12
-4 3

Sample Output

111
1B
-11

#include <iostream>#include <cstring>using namespace std;int R;char bit[] = "0123456789ABCDEF";char s[100];void base_change(int N){    if (N/R == 0) {        s[0] = bit[N%R];        return;    } else {        base_change(N/R);        int len = strlen(s);        s[len] = bit[N%R];        ++len;        s[len] = '\0';    }}int main(){    int N;    while (cin>>N>>R) {        memset(s, '\0', sizeof(s));        int neg = 0;        if (N < 0) {            N = -N;            neg = 1;        }        if (neg) {            cout<<"-";        }        base_change(N);        cout<<s<<endl;    }    return 0;}
#include <iostream>using namespace std;int main(void){    int N;    int R;    int i;    int flag = 0;    int wei[10000] = {0};    int num = 0;    while (cin>>N>>R) {        num = 0;        flag = 0;        if (N < 0) {            N = -N;            flag = 1;        }        while (N > 0) {            wei[num] = N % R;            num++;            N = N / R;        }        if (1 == flag) {            cout<<"-";        }        for (i=num-1; i>=0; i--) {            if (wei[i] < 10) {                cout<<wei[i];            } else {                cout<<(char)('A' + wei[i] - 10);            }        }        cout<<endl;    }    return 0;}
#include <iostream>#include <cstring>using namespace std;int R;char bit[] = "0123456789ABCDEF";char s[100];int len;void base_change(int N){    if (N/R == 0) {        s[0] = bit[N%R];        len = 1;        return;    } else {        base_change(N/R);        s[len] = bit[N%R];        ++len;    }}int main(){    int N;    while (cin>>N>>R) {        memset(s, '\0', sizeof(s));        int neg = 0;        if (N < 0) {            N = -N;            neg = 1;        }        if (neg) {            cout<<"-";        }        base_change(N);        cout<<s<<endl;    }    return 0;}
0 0
原创粉丝点击