zoukankan      html  css  js  c++  java
  • 北京理工大学复试上机--2002

    1、某人有8 角的邮票5 张,1 元的邮票4 张,1 元8 角的邮票6 张,用这些邮票中的一张或若干张可以得到多少中不同的邮资?
    #include <iostream>
    #include <set>
    using namespace std;
    int main() {
        set<int> s;
        int i, j, k;
        for(i = 0; i <= 5; i++) {
            for(j = 0; j <= 4; j++) {
                for(k = 0; k <= 6; k++) {
                    s.insert(8 * i + 10 * j + 18 * k);
                }
            }
        }
        cout << s.size() - 1;
        return 0;
    }
    2、输入 n 值,使用递归函数,求杨辉三角形中各个位置上的值,按照如下形式打印输出图形。例如:当n=6 时。
          1
        1  1
       1 2  1
      1 3 3  1
     1 4  6  4 1
    1 5 10 10 5 1
    #include <iostream>
    using namespace std;
    
    int yh(int x, int y) {
        if(x == y || y == 1) return 1;
        else return yh(x - 1, y - 1) + yh(x - 1, y);
    }
    
    int main() {
        int n;
        while(cin >> n) {
            for(int i = 0; i < n; i++) {
                for(int j = i + 1; j < n; j++) {
                    cout << " ";
                }
                for(int j = 0; j <= i; j++) {
                    cout << yh(i + 1, j + 1) << " ";
                }
                cout << endl;
            }
        }
        return 0;
    }
     3、打印所有不超过 n(n<256)的,其平方具有对称性质的数。如 11*11=121。
    #include <iostream>
    using namespace std;
    
    bool sym(int n) {
        int m = n;
        int a[20], i = 0, sum = 0, x = 1;
        while(m) {
            a[i++] = m % 10;
            m /= 10;
        }
        for(int j = i - 1; j >= 0; j--) {
            sum += a[j] * x;
            x *= 10;
        }
        return sum == n;
    }
    int main() {
        for(int i = 1; i <= 256; i++) {
            if(sym(i * i)) cout << i << " ";
        }
        return 0;
    }
    4、编写一个求菲波那奇数列的递归函数,输入 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 fib(int n) {
        if(n == 0) return 0;
        else if(n == 1) return 1;
        return fib(n - 1) + fib(n - 2);
    }
    
    int main() {
        int n;
        while(cin >> n) {
            for(int i = 0; i < n; i++) {
                for(int j = i + 1; j < n; j++) {
                    cout << " ";
                }
                for(int j = 0; j < 2 * i + 1; j++) {
                    cout << fib(j) << " ";
                }
                cout << endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    mvn常用命令
    maven pom.xml解释 (转)
    hibernate的主键生成策略
    软件绿色版和安装版的区别
    spring事务
    JdbcTemplate操作数据库
    控制反转和spring在项目中可以带来的好处
    三种实例化bean的方式
    UVA 1262 Password 暴力枚举
    CSDN2015博客之星评选之拉票环节
  • 原文地址:https://www.cnblogs.com/ache/p/12520088.html
Copyright © 2011-2022 走看看