首页 > 其他分享 >中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题

中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题

时间:2024-03-17 20:04:36浏览次数:21  
标签:MOOC 自测题 符号 int 打印 number 陈越 num printf

自测-1 打印沙漏

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

*****
 ***
  *
 ***
*****

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

代码实现

#include<stdio.h>

int main() {
    int n; // 输入的符号数量
    char c; // 要打印的符号
    scanf("%d %c", &n, &c); // 读取输入

    int sum = 0; // 用于存储每一行的符号数量总和
    int i = 1; // 用于循环的变量,初始为1,每次递增2
    // 计算沙漏的高度(从顶部到中心)并找到对应的行数
    for (i = 3;; i += 2) {
        sum += i; // 增加当前行的符号数量
        if (sum * 2 + 1 > n) { // 如果总符号数量超过输入数量,则退出循环
            i = i - 2; // 回退一行
            break;
        }
    }
    int temp = i; // 临时变量,用于保存沙漏的高度

    int count = 0; // 用于计算每行需要打印的符号数量
    // 打印沙漏的上半部分
    while (i > 0) {
        count = 0; // 初始化每行的符号数量
        for (int j = i; j > 0; j--) {
            count++; // 增加当前行的符号数量
        }
        // 打印每行前面的空格
        for (int q = 0; q < (temp - count) / 2; q++) printf(" ");
        // 打印当前行的符号
        for (int j = i; j > 0; j--) {
            printf("%c", c);
            count++;
        }
        printf("\n"); // 换行
        i = i - 2; // 递减,准备打印下一行
    }

    i = i + 4; // 恢复 i 的值,用于打印沙漏的下半部分
    // 打印沙漏的下半部分
    while (i <= temp) {
        count = 0; // 初始化每行的符号数量
        for (int j = i; j > 0; j--) {
            count++; // 增加当前行的符号数量
        }
        // 打印每行前面的空格
        for (int q = 0; q < (temp - count) / 2; q++) printf(" ");
        // 打印当前行的符号
        for (int j = i; j > 0; j--) {
            printf("%c", c);
            count++;
        }
        printf("\n"); // 换行
        i = i + 2; // 递增,准备打印下一行
    }
    // 计算并打印剩余的符号数量
    sum = 0;
    while (temp > 1) {
        sum += temp; // 增加当前行的符号数量总和
        temp -= 2; // 减少行数
    }
    sum = sum * 2 + 1; // 计算剩余符号数量
    printf("%d", n - sum); // 打印剩余符号数量
    return 0;
}

自测-2 素数对猜想

让我们定义dn​为:dn​=pn+1​−pn​,其中pi​是第i个素数。显然有d1​=1,且对于n>1有dn​是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

代码实现

#include<stdio.h>
#include<math.h>

// 判断一个数是否是素数
int is_prime(int n){
    if(n < 2) return 0;
    for(int i = 2; i <= sqrt(n); i++){
        if(n % i == 0) return 0;
    }
    return 1;
}

int main() {
    int n, count = 0;
    scanf("%d", &n);
    int prev_prime = 2; // 记录前一个素数
    // 遍历小于N的每个数,检查是否满足猜想
    for(int i = 3; i <= n; i++){
        if(is_prime(i)){
            if(i - prev_prime == 2){ // 检查差值是否为2
                count++;
            }
            prev_prime = i; // 更新前一个素数
        }
    }
    printf("%d", count);
    return 0;
}

自测-3 数组元素循环右移问题

一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:

每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。

输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

代码实现

#include<stdio.h>

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    int a[n];
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    m = m % n; // Ensure m is within the range of array size

    // 输出循环右移后的数组
    for(int i = n - m; i < n; i++) {
        printf("%d ", a[i]);
    }
    for(int i = 0; i < n - m; i++) {
        if(i < n - m - 1)
            printf("%d ", a[i]);
        else
            printf("%d", a[i]);
    }
    
    return 0;
}

自测-4 Have Fun with Numbers

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

code

#include <stdio.h>
#include <string.h>

int main() {
    char num[22] = {0}; // 存储输入数字的字符数组,额外留出一位来处理可能的进位
    scanf("%s", num);   // 读取输入数字
    int jinwei = 0;     // 进位
    int count[10] = {0}; // 用于统计每个数字出现的次数的数组,下标表示数字本身
    // 从右往左处理输入数字并将每个数字加倍
    for (int i = strlen(num) - 1; i >= 0; i--) {
        int digit = num[i] - '0'; // 将字符数字转换为整数
        count[digit]++;           // 更新数字出现次数计数器
        int doubled_digit = digit * 2 + jinwei; // 将数字加倍并加上进位
        jinwei = doubled_digit / 10; // 计算进位
        num[i] = doubled_digit % 10 + '0'; // 将加倍后的数字转换回字符并存储
    }

    // 处理可能的进位
    if (jinwei > 0) {
        memmove(num + 1, num, strlen(num) + 1); // 将所有字符后移一位
        num[0] = jinwei + '0'; // 将进位添加到首位
    }

    // 检查加倍后的数字是否是原始数字的一个排列
    int flag = 0; // 标志位,用于标记是否存在不匹配的数字
    for (int i = 0; i < strlen(num); i++) {
        int digit = num[i] - '0'; // 将字符数字转换为整数
        count[digit]--; // 减去每个数字的出现次数
        if (count[digit] < 0) { // 若某个数字出现次数小于0,则说明不匹配
            flag = 1; // 将标志位设置为1
            break; // 跳出循环
        }
    }

    // 打印结果
    if (flag == 0) {
        printf("Yes\n"); // 若不存在不匹配的数字,则打印Yes
    } else {
        printf("No\n"); // 否则打印No
    }
    printf("%s\n", num); // 打印加倍后的数字

    return 0;
}

自测-5 Shuffling Machine

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, 
H1, H2, ..., H13, 
C1, C2, ..., C13, 
D1, D2, ..., D13, 
J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

code

#include <stdio.h>

#define TOTAL_CARDS 54

void shuffle(int order[], char cards[][4]) {
    char temp[TOTAL_CARDS][4]; // 创建一个临时数组来存储洗牌后的顺序
    for (int i = 1; i <= TOTAL_CARDS; ++i) {
        for (int j = 0; j < 4; ++j) {
            temp[order[i] - 1][j] = cards[i - 1][j]; // 根据给定顺序进行洗牌
        }
    }
    for (int i = 1; i <= TOTAL_CARDS; ++i) {
        for (int j = 0; j < 4; ++j) {
            cards[i - 1][j] = temp[i - 1][j]; // 将洗牌后的结果复制回原数组
        }
    }
}

int main() {
    int K; // 重复次数
    scanf("%d", &K);

    int order[TOTAL_CARDS + 1]; // 给定的顺序
    for (int i = 1; i <= TOTAL_CARDS; ++i) {
        scanf("%d", &order[i]);
    }

    char cards[TOTAL_CARDS][4] = {
        "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13",
        "H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13",
        "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13",
        "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13",
        "J1", "J2"
    }; // 初始的扑克牌顺序

    for (int k = 0; k < K; ++k) {
        shuffle(order, cards); // 重复洗牌
    }

    // 输出结果
    for (int i = 0; i < TOTAL_CARDS; ++i) {
        printf("%s", cards[i]);
        if (i != TOTAL_CARDS - 1) {
            printf(" ");
        }
    }

    return 0;
}

标签:MOOC,自测题,符号,int,打印,number,陈越,num,printf
From: https://blog.csdn.net/m0_57715084/article/details/136722549

相关文章