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

    1、编写程序,计算下列分段函数 y=f(x)的值。
    y = -x + 2.5, 0 <= x < 2
    y = 2 - 1.5 (x - 3) (x - 3), 2 <= x < 4
    y = x / 2 - 1.5, 4 <= x < 6
    #include <iostream>
    using namespace std;
    int main() {
        double x;
        while(cin >> x) {
            if(x >= 0 && x < 2) {
                cout << 2.5 - x << endl;
            }
            else if(x >= 2 && x < 4) {
                cout << 2 - 1.5 * (x - 3) * (x - 3) << endl;
            }
            else if(x >= 4 && x < 6) {
                cout << x / 2 - 1.5 << endl;
            }
            else {
                cout << "请输入0-6的数字";
                continue;
            }
        }
        return 0;
    }
    2、编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。
    #include <iostream>
    using namespace std;
    int main() {
        int n;
        while(cin >> n) {
            int sum = 0;
            if(n > 0) {
                for(int i = n; i <= 2 * n; i++) {
                    sum += i;
                }
            }
            else {
                for(int i = -n; i <= 2 * (-n); i++) {
                    sum -= i;
                }
            }
            cout << sum << endl;
        }
        return 0;
    }
    3、设 N 是一个四位数,它的 9 倍恰好是其反序数(例如:1234 的反序数是 4321),求 N 的值。
    #include <iostream>
    using namespace std;
    
    int main() {
        int n, m;
        for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++) {
                for(int k = 0; k < 10; k++) {
                    for(int l = 0; l < 10; l++) {
                        n = i * 1000 + j * 100 + k * 10 + l;
                        m = l * 1000 + k * 100 + j * 10 + i;
                        if(n * 9 == m && m > 0) cout << n << endl; //无限制m>0,会多输出0
                    }
                }
            }
        }
        return 0;
    }
    4、N 个人围成一圈顺序编号,从 1 号开始按 1、2、3 顺序报数,报 3 者退出圈外,其余的人再从 1、2、3 开始报数,报 3 的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环形链表编程。
     
    #include <iostream>
    using namespace std;
    
    struct Node
    {
        int data;
        Node* next;
    };
    
    int main() {
        int m;
        cin >> m;
        while (m--) {
            int n;
            cin >> n;
            Node *head = new Node;
            Node *p = new Node;
            head = p;
            p -> next = NULL;
            p -> data = 1;
            for(int i = 2; i <= n; i++) {
                Node *q = new Node;
                q -> data = i;
                q -> next = NULL;
                p -> next = q;
                p = q;
            }
            p -> next = head;
            int cnt = 1;
            while(head -> next != head) {
                Node *t = new Node;
                t = head -> next;
                cnt++;
                if(cnt % 3 == 0) {
                    cout << head -> next -> data << " ";
                    head -> next = t -> next;
                }
                else head = head -> next;
            }
            cout << head -> data << endl;
        }
        return 0;
    }
  • 相关阅读:
    人工神经网络(Artificial Neural Networks)
    潜语义分析(Latent Semantic Analysis)
    IOS Dictionary和Model相互转换
    jquery ajax跨域请求webservice
    日期格式转换
    1
    iptables详解
    yum报错-Network is unreachable"Error:
    41个Web开发者JavaScript实用小技巧
    比较常用的几个maven第三方镜像
  • 原文地址:https://www.cnblogs.com/ache/p/12518950.html
Copyright © 2011-2022 走看看