北京理工大学复试上机--2013

1、求两个数的最大公约数

示例:

输入:24 18

输出:6

#include <iostream>
#include <math.h>
using namespace std;
int main() {
    int a, b, i, j, m;
    while (cin >> a >> b) {
        m = min(a, b);
        j = 0;
        for (i = 1; i <= m; i++) {
            if (a % i == 0 && b % i == 0) {
                if (i > j) j = i;
            }
        }
        cout << j << endl;
    }
    return 0;
}

2、输入-组英文单词,按字典顺序(大写与小写字母具有相同大小写)

排序输出.

示例:

输入: Information Info Inform info Suite suite suit

输出: Info info Inform Information suit Suite suite

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

bool cmp(string s1, string s2)
{
    for(int i = 0; i < s1.length(); i++) {
        s1[i] = tolower(s1[i]);
    }
    for(int i = 0; i < s2.length(); i++) {
        s2[i] = tolower(s2[i]);
    }
    return s1 < s2;
}

int main()
{
    int num = 0;
    string s, str;
    vector<string> v, vv;
    map<string, int> m;
    while (getline(cin, s)) {
        for (int i = 0; i < s.length(); i++) {
            if (isalpha(s[i])) str += s[i];
            if (s[i] == ‘ ‘ || i + 1 == s.length()) {
                v.push_back(str);
                str = "";
            }
        }
        sort(v.begin(), v.end(), cmp);
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " ";
        }
        cout << endl;
        v.clear();
    }
    return 0;
}

3、编写程序:输入表达式,输出相应二叉树的先序遍历结果(干脆中缀转前缀 后缀都给出了 ,有示例)
输入: a+b*(c- -d)-e/f
输出: -+a*b-cd/ef
infix: a+b-a*((c+d)/e-f)+g
suffix: ab+acd+e/f-*-g+
prefix: +-+ab*a-/+cdefg

#include <iostream>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

string toSuf(string s)
{
    map<char, int> isp, icp; //isp--in stack preority icp--in coming preority
    isp[‘(‘] = 1; isp[‘*‘] = 5; isp[‘/‘] = 5; isp[‘+‘] = 3; isp[‘-‘] = 3; isp[‘)‘] = 6;
    icp[‘(‘] = 6; icp[‘*‘] = 4; icp[‘/‘] = 4; icp[‘+‘] = 2; icp[‘-‘] = 2; icp[‘)‘] = 1;
    string sufstr;
    stack<char> opt; //operator
    for (int i = 0; i < s.length(); i++) {
        if (isalpha(s[i]) || (s[i] >= ‘0‘ && s[i] <= ‘9‘)) {
            sufstr += s[i];
        }
        else if (opt.empty() || icp[s[i]] > isp[opt.top()]) {
            opt.push(s[i]);
        }
        else {
            if (s[i] == ‘)‘) {
                while (opt.top() != ‘(‘) {
                    sufstr += opt.top();
                    opt.pop();
                }
                opt.pop();
            }
            else {
                while (!opt.empty() && isp[opt.top()] >= icp[s[i]]) {
                    sufstr += opt.top();
                    opt.pop();
                }
                opt.push(s[i]);
            }
        }
    }
    while (!opt.empty()) {
        sufstr += opt.top();
        opt.pop();
    }
    return sufstr;
}

string toPre(string s)
{
    map<char, int> isp, icp; //isp--in stack preority   icp--in coming preority
    isp[‘(‘] = 6; isp[‘*‘] = 4; isp[‘/‘] = 4; isp[‘+‘] = 2; isp[‘-‘] = 2; isp[‘)‘] = 1;
    icp[‘(‘] = 1; icp[‘*‘] = 5; icp[‘/‘] = 5; icp[‘+‘] = 3; icp[‘-‘] = 3; icp[‘)‘] = 6;
    string prestr;
    stack<char> opt; //operator
    for (int i = s.length() - 1; i >= 0; i--) {
        if (isalpha(s[i]) || (s[i] >= ‘0‘ && s[i] <= ‘9‘)) {
            prestr += s[i];
        }
        else if (opt.empty() || icp[s[i]] >= isp[opt.top()]) {
            opt.push(s[i]);
        }
        else {
            if (s[i] == ‘(‘) {
                while (opt.top() != ‘)‘) {
                    prestr += opt.top();
                    opt.pop();
                }
                opt.pop();
            }
            else {
                while (!opt.empty() && isp[opt.top()] > icp[s[i]]) {
                    prestr += opt.top();
                    opt.pop();
                }
                opt.push(s[i]);
            }
        }
    }
    while (!opt.empty()) {
        prestr += opt.top();
        opt.pop();
    }
    reverse(prestr.begin(), prestr.end());
    return prestr;
}

int main()
{
    int l;
    string s, sufstr, prestr;
    cout << "Infix:  ";
    while (getline(cin, s)) {
        sufstr = toSuf(s);
        prestr = toPre(s);
        cout << "Suffix: " << sufstr << endl
             << "Prefix: " << prestr << endl;
        cout << endl << "Infix:  ";
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ache/p/12571719.html

时间: 2024-08-02 08:34:27

北京理工大学复试上机--2013的相关文章

北京理工大学复试上机--2009

1.请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如 输入:EricZ 输出:1=EricZ 输入:David 输出:1=David 2=EricZ 输入:Peter 输出:1=Peter 2=David 3=EricZ 输入:Alan 输出:1=Alan 2=Peter 3=David 4=EricZ 输入:Jane 输出:1=Jane 2=Alan 3=Peter 4=David 2.把上述最后结果保存到Name.txt中; #include <iostream> #i

北京理工大学复试上机--2010

1.输入一串整数,输入命令排序! 输入 a t 在这串整数后面添加整数 t, 输入 c\m\n 有 n 替换 m, 输入 d t 删除 t, 输入 s 排序. #include <iostream> #include <vector> #include <cstring> #include <algorithm> using namespace std; int tonum(string s, int l) { int n = 1, sum = 0; for

北京理工大学复试上机--2016

1.输入学生信息,姓名成绩(成绩的数目不一定)输出每个学生的学号和平均成绩,以及不及格课程数超过2的学生,按不及格课程数从大到小排好序输出. input: stu1 60 70 80 30 stu2 10 20 30 40 50 stu3 10 20 30 40 50 60 30 stu4 60 80 100 stu5 50 40 30 60 70 # output: stu1 60 stu2 30 stu3 34.2857 stu4 80 stu5 50 不及格课程数超过2的学生有: stu3

北京理工大学复试上机--2020

PS: 2020的是夏令营试题 1.题目:给你一个 m*n 大小的矩阵,每个点有 0,1,2 三种取值:0 代表障碍物,1代表白纸,2 代表墨滴.每一秒墨滴可以向其上下左右扩散,将四周的白纸染色,被染色之后的白纸可以继续向四周扩散,以此类推.问经过几秒,矩阵中所有的白纸都被染色. 如果可以,则输出扩散时间: 如果不可以,则输出FALSE. 输入: m n 的大小以及矩阵每个点的值 输出: 扩散时间 或 FALSE 例如: 3 3 0 1 0 1 2 1 0 1 0 输出: 1 3 3 0 1 0

北京理工大学复试上机--2019

1.字符串解析将字符串看成不同的字符切片,切片不可重复,按字母序输出所有切片(每个切片一行) 输入: aaabbcaaabaa 输出: aa aaa b bb c #include <iostream> #include <set> using namespace std; int main() { string s; while (cin >> s) { set<string> ss; string str; int i; for (i = 0; i &l

北理工计算机复试上机 2013

1. 求两个数的最大公约数(似乎有个辗转相除法,为什么不用呢,没错,我不会) 示例: 输入:24,18 输出:6 1 // 2013_1.cpp : Defines the entry point for the console application. 2 // 3 #include<iostream> 4 using namespace std; 5 6 int main(int argc, char* argv[]) 7 { 8 int a,b; 9 cout<<"

HDU 1234 (浙大计算机研究生复试上机考试-2005年) 开门人和关门人 (水)

开门人和关门人 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11133    Accepted Submission(s): 5667 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

北京航空航天大学计算机系考研复试上机真题及答案---2014

第一题,阶乘数. 输入一个正整数,输出时,先输出这个数本身,跟着一个逗号,再输出这个数的各位数字的阶乘和,等号,阶乘和的计算结果,并判断阶乘和是否等于原数,如果相等输出Yes,否则输出No.题目说明输入的正整数以及其各位阶乘和都不会超出int型的表示范围. 输入样例1: 145 输出样例1: 145,1!+4!+5!=145 Yes 输入样例2: 1400 输出样例2: 1400,1!+4!+0!+0!=27 No 第二题,五子棋. 输入一个19*19的矩阵,只包含数字0.1.2,表示两人下五子