2017内大892专硕真题编程题

在这里插入图片描述

#include <iostream>

using namespace std;

int main() {
    int a, b, c;
    int max;
    cin >> a >> b >> c;
    if (a >= b) {//比较a b哪个大,保存大的那个
        max = a;
    } else{
        max = b;
    }
    if (max >= c) {//比较ab大的那个和c谁大
        cout << max << endl;
    } else {
        cout << c << endl;
    }
}

在这里插入图片描述

//最大公约数用辗转相除法算出后,用a*b/最大公约数=最小公倍数
//a>b,a%b=c,只要c!=0,则a = b, b = c循环,当c=0之后,则最大公约数等于b;
//法一:非递归方法
#include <iostream>

void swab(int *, int *);

using namespace std;

int main() {
    int a, b, c;
    int m ,n;
    cin >> a >> b;
    m = a, n = b;
    if (m < n) {
        swab(&m, &n);
    }
    c = m % n;
    while (c != 0) {
        m = n;
        n = c;
        c = m % n;
    }
    cout << "最大公约数 = " << n << endl;
    cout << "最小公倍数 = " << a*b/n << endl;
}

void swab(int *i, int *i1) {
    int t;
    t = *i;
    *i = *i1;
    *i1 = t;
}
//法二:递归方法
#include <iostream>

void swab(int *a, int *b);

int getGYS(int a, int b);

using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b;
    if (a < b) {
        swab(&a, &b);
    }
    c = getGYS(a,b);
    cout << "最大公约数 = " << c << endl;
    cout << "最小公倍数 = " << a * b / c << endl;
}

int getGYS(int a, int b) {
    return a % b == 0 ? b : getGYS(b, a % b);
}

void swab(int *a, int *b) {
    int t;
    t = *a;
    *a = *b;
    *b = t;
}

在这里插入图片描述

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    for (int i = a + 1; i < b; ++i) {
        int j = 2;
        for (; j <= sqrt(i); ++j) {
            if (i % j == 0) {
                break;
            }
        }
        if (j > sqrt(i)) {
            cout << i << " ";
        }
    }
}

在这里插入图片描述

#include <iostream>

using namespace std;

int main() {
    double sum = 0;
    double i = 1, j = 2, tmp;
    int n;
    cin >> n;
    for (int k = 0; k < n; ++k) {
        double fenshu = j/i;
        sum += fenshu;
        tmp = j + i;
        i = j;
        j = tmp;
    }
    cout << sum;
}

在这里插入图片描述

#include <iostream>

using namespace std;

int MyStrlen(char *str) {
    int sum = 0;
    while (*str) {
        if (*str <= 'Z' && *str >= 'A' || *str <= 'z' && *str >= 'a') sum++;
        str++;
    }
    return sum;
}

int main() {
    string str;
    getline(cin, str);
    int num = MyStrlen(&str[0]);
    cout << num;
}

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

void reverse(char * str) {
    if (*str == '.') {
        return;
    } else{
        reverse(str + 1);
        cout << *str;
    }
}

int main() {
    string str;
    cin >> str;
    reverse(&str[0]);
}

在这里插入图片描述

#include<iostream>

bool ifLeap(int A);

using namespace std;

int main(void) {
    int A, B, C;
    int X, Y, Z;
    int res = 0;
    int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    cin >> A >> B >> C;
    cin >> X >> Y >> Z;
    if (A == X){
        //如果年份一样
        if (ifLeap(A)) {
            days[2] = 29;
        }
        if (B == Y) {
            //如果月份一样
            cout << Z - C + 1;
            return Z - C + 1;
        } else{
            //月份不同
            for (int i = B; i < Y; ++i) {
                res+=days[i];
            }
            res = res - C + Z + 1;
            cout << res;
            return 0;
        }
    } else{
        //如果年份不同
        for (int i = A; i < X; ++i) {
            if (ifLeap(A)) {
                res+=366;
            } else{
                res+=365;
            }
        }
        if (ifLeap(A)) {
            days[2] = 29;
        }
        for (int j = 1; j < B; ++j) {
            res-=days[j];
        }
        res-=C;
        if (!ifLeap(X)) {
            days[2] = 28;
        }
        for (int k = 1; k < Y; ++k) {
            res += days[k];
        }
        res += Z;
        cout << res + 1;
        return 0;
    }
}

bool ifLeap(int A) { return A % 4 == 0 && A % 100 != 0 || A % 400 == 0; }

在这里插入图片描述

#include <iostream>
#include <string>

void sort(char *s1, char *s2);

using namespace std;

int main() {
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2);
    sort(&s1[0], &s2[0]);
}

void sort(char *s1, char *s2) {
    char sortedArr[1000];
    int flag = 0;
    while (*s1&&*s2) {
        if (*s1 <= *s2) {
            sortedArr[flag++] = *s1++;
        } else{
            sortedArr[flag++] = *s2++;
        }
    }
    while (*s1) {
        sortedArr[flag++] = *s1++;
    }
    while (*s2) {
        sortedArr[flag++] = *s2++;
    }
    for (int i = 0; i < flag; ++i) {
        cout << sortedArr[i] << " ";
    }
}

在这里插入图片描述
很抱歉之前这道题答案错了,因为没有在循环内部进行学生数量的判断,所以造成了死循环。

#include <iostream>
#include <string>

using namespace std;

int main() {
    int n, i, k;
    int count = 0;//计算出列了多少学生
    cin >> n >> i >> k;
    int step = 1;//记录每k步出列一个学生
    int index = i;//记录现在是第几个学生
    int *arr = new int[n + 1];
    for (int j = 0; j <= n; ++j) {
        //把0到n赋值到指针数组里面,注意第0个位置是0,其实相当于没学生,这样做的目的是为了用户输入第几个元素,不用考虑数组是从零开始
        arr[j] = j;
    }
    //只要没取出n个学生,步数不等于k+1,就继续取学生
    while (count != n) {
        //第二层while控制必须出列一个学生,如果写成了step!=k,则到step=k-1的时候最后会step++,则没到第k位置就退出了二层while循环
        if (arr[index] != 0) {
            //如果当前值不等于0,说明没出列,设置为0则表示该学生已经出列
            if (step == k) {
                //如果步数已经等于k,则学生出列
                cout << arr[index] << " ";//为了打印用
                arr[index] = 0;//把该位置学生的数值设置为0,表示学生已经出列
                step = 0;//找到学生了当然要重置步数
                count++;//出队学生数+1
            }
            step++;
            index = (index + 1) % (n + 1);//不对n取余则每次只能从arr[0]遍历到arr[n]
        } else {
            //如果当前值为0,表示学生已经出列,step不能自增,因为已经出列的学生不能报数
            index = (index + 1) % (n + 1);
        }
    }
}
  • 10
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bean冷的心

你的鼓励将是我前进的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值