复试机试【XN2018】

计科学硕

                    1.将一个长度为10的数列,最大值放第一个,最小值放最后一个。
                    输入:1 2 3 4 5 9 0 7 8 6
                    输出:9 2 3 4 5 1 6 7 8 0

#include <iostream>;
using namespace std;

int num[10];

int main() {
    for (int i = 0; i <= 9; i++) {
        cin >> num[i];
    }

    int maxValue = -99999;
    int maxIndex = 0;
    int minValue = 99999;
    int minIndex = 0;

    for (int i = 0; i <= 9; i++) {
        if (num[i] > maxValue){
            maxValue = num[i];
            maxIndex = i;
        }else if (num[i] < minValue){
            minValue = num[i];
            minIndex = i;
        }
    }

    swap(num[0], num[maxIndex]);
    swap(num[9], num[minIndex]);

    for (int i = 0; i <= 9; i++) {
        cout << num[i] << " ";
    }
    cout << endl;

}


                    2.输入几个数,把素数找出,由大到小排序输出。
                    输入:6 11 5 10 13 35 9
                    输出:5 11 13


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


bool isPrime[1001];
vector<int> prime;

void initialize(int maxIndex) {
    // 0 1 不是素数,2 ~ 最大下标 默认是素数
    isPrime[0] = isPrime[1] = 0;
    for (int i = 2; i <= maxIndex; i++) {
        isPrime[i] = 1;
    }


    // 素数的倍速,不是素数
    for (int i = 2; i <= maxIndex; i++) {
        if (isPrime[i] == 1) {
            for (int j = 2 * i; j <= maxIndex; j += i) {
                isPrime[j] = 0;
            }
        }

    }
    return ;
}

int toInt(string s) {
    stringstream ss(s);
    int a ;
    ss >> a;
    return a;
}




int main() {
    initialize(1000);

    string s;
    string sub;
    int t;
    getline(cin, s);

    int index = s.find(" ");
    while (index != -1) {
        sub = s.substr(0, index); // pos len
        t = toInt(sub);
        //cout << t << endl;
        if (isPrime[t]) {
            prime.push_back(t);
        }

        s.erase(0, index + 1); // pos len, 连带空格也删掉
        index = s.find(" ");
    }
    if (s.size() > 0) {
        t = toInt(s);
        if (isPrime[t]) {
            prime.push_back(t);
        }
    }
    //cout << "ok" << endl;
    //cout << prime.size() << endl;
    sort(prime.begin(), prime.end());
    for (int i = 0; i <= prime.size() - 1; i++){
        cout << prime[i] << " ";
    }
    cout << endl;

}


                    3.输入考生的学号、姓名、考试成绩。输入5个考生信息,按照成绩大小升序和逆序输出。使用指针函数,函数调用和结构体。

指针函数 == 返回值为 指针

函数指针 ==一个指针变量,该指针指向这个函数

指针函数本质是一个函数,其返回值为指针。
函数指针本质是一个指针,其指向一个函数。

void* : 可以用其 指代任何类型的指针。 可以当int* double* 使用!

因为指针只占1个字节的空间,存放的是首地址,到底是int的首地址,还是double的首地址,看具体情况。

使用时:不能用void指针直接进行操作;只能转换成对应类型指针后,才能操作

    int a = 5;
    void* p = & a;
    cout << * (int*)p  << endl;


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
using namespace std;

// Student 代表结构体
// PStudent 代表 结构体的指针,开辟内存空间专门存放内存地址,存放Student变量的第一个地址。
typedef struct Student {
    int no;
    string name;
    double score;
}Student, * PStudent;

vector<PStudent> res; // 存放student的指针

PStudent getPStudent(int no, string name, double score) {
    PStudent t = (PStudent)malloc(sizeof(Student));
    (*t).no = no;
    (*t).name = name;
    (*t).score = score;
    return t;
}

bool compare(PStudent a, PStudent b) {
    return (*a).score < (*b).score;
}


int main() {
    int no;
    string name;
    double score;
    for (int i = 1; i <= 5; i++) {
        cin >> no >> name >> score ;
        PStudent t = getPStudent(no, name, score);
        res.push_back(t);
    }

    sort(res.begin(), res.end(), compare);
    for (int i = 0 ; i <= res.size() - 1; i++) {
        cout << (*res[i]).no << " " << (*res[i]).name << " " << (*res[i]).score << endl;
    }
    for (int i = res.size() - 1 ; i >= 0; i--) {
        cout << (*res[i]).no << " " << (*res[i]).name << " " << (*res[i]).score << endl;
    }
}
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void show(int a){
    cout << "a is " << a << endl;
}
void (*p_show)(int a);

void getHello(){
    cout << "hello" << endl;
    return ;
}
void (*p_getHello)();

int main() {
    p_show = show;
    (*p_show)(255);

    p_getHello = getHello;
    (*p_getHello)();
}



软工农信:1.输入一个整数n,输出该整数中重复的数字,如果没有重复出现的数字则输出 No repeat number! 
                    输入:2312626
                    输出:2 6

#include <iostream>

using namespace std;

long long n;
int num[10]; // 0 ~ 9
int cnt;

int main(){

    for (int i = 0; i <= 9; i++){
        num[i] = 0;
    }
    cnt = 0;

    cin >> n;
    while (n > 0){
        int wei = n % 10;
        num[wei] ++;
        cnt++;
        //cout << wei << endl;
        n /= 10;
    }

    if (!cnt){
        cout << "No repeat number!" << endl;
    }else {
        for (int i = 0; i <= 9; i++){
            if (num[i] >= 2){
                cout << i << " ";
            }
        }
        cout << endl;
    }




}


                    2.输入一个字符串,统计字符串中大、小写字母,数字及其他字符出现的次数
                    输入:abcd123![]ABC
                    输出:3 4 3 3

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int cntUp = 0;
int cntLow = 0;
int cntNum = 0;
int cntOther = 0;

int main() {
    string s;
    cin >> s;
    for (int i = 0; i <= s.size() - 1; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            cntLow++;
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
            cntUp++;
        } else if (s[i] >= '0' && s[i] <= '9') {
            cntNum++;
        } else {
            cntOther++;
        }

    }
    printf("%d %d %d %d\n", cntUp, cntLow, cntNum, cntOther);


}


                    3.输入5个字符串,对字符串排序并输出
 

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

vector<string> res;

bool compare(string s1, string s2){
    return s1[0] < s2[0];
}


int main(){

    res.clear();

    string t;
    for (int i = 1; i <= 5; i++){
        cin >> t;
        res.push_back(t);
    }

    sort(res.begin(), res.end(), compare);
    for (int i = 0; i <= res.size() - 1; i++){
        cout << res[i] << " ";
    }
    cout << endl;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值