BIT-CS-2002年复试机试(B)

2019-01-29  本文已影响6人  小白之白小明

1、打印所有不超过 n(n<256)的,其平方具有对称性质的数。如 11*11=121。

#include<iostream>
using namespace std;
int main()
{
    int num, t, j[10],temp;
    for (int i = 1; i <= 256; i++) {
        num = 0;   //num记录t的位数
        t = i*i;
        temp = 0;
        while (t) {
            j[num] = t % 10;   //j[]记录t的每一位数
            num++;
            t = t / 10;
        }
        for (int b = 0; b <num; b++){
            temp = temp * 10 + j[b];
        }
        if (temp == i*i)
            cout << i << endl;
    }
    system("pause");
    return 0;
}

2、编写一个求菲波那奇数列的递归函数,输入 n值,使用该递归函数,输出如下图形。例如:当n=6时。

                  0

               0 1 1

            0 1 1 2 3

         0 1 1 2 3 5 8

      0 1 1 2 3 5 8 13 21

 0 1 1 2 3 5 8 13 21 34 55
#include<iostream>
using namespace std;
int fun(int i, int j) {
    if (j == 1)
        return 0;
    else if (j == 2)
        return 1;
    else
        return fun(i, j - 1) + fun(i, j - 2);
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 2*n-2*i; j >= 1; j--)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << fun(i, j) << " ";
        cout << endl;
    }
    system("pause");
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读