小工具知识网

当前位置: 首页 > 行业知识 > 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题

行业知识

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

2026-11-29 23:33:17 来源:互联网转载

自测-1 打印沙漏

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

()
***** ***  * ********

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

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

()

输入格式:

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

输出格式:

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

代码实现

#includeint 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  0; j--) {            printf("%c", c);            count++;        }        printf("\n"); // 换行        i = i - 2; // 递减,准备打印下一行    }    i = i + 4; // 恢复 i 的值,用于打印沙漏的下半部分    // 打印沙漏的下半部分    while (i  0; j--) {            count++; // 增加当前行的符号数量        }        // 打印每行前面的空格        for (int q = 0; q  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( 0) { memmove(num + 1, num, strlen(num) + 1); // 将所有字符后移一位 num[0] = jinwei + '0'; // 将进位添加到首位 } // 检查加倍后的数字是否是原始数字的一个排列 int flag = 0; // 标志位,用于标记是否存在不匹配的数字 for (int i = 0; i

自测-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 #define TOTAL_CARDS 54void shuffle(int order[], char cards[][4]) {    char temp[TOTAL_CARDS][4]; // 创建一个临时数组来存储洗牌后的顺序    for (int i = 1; i 

上一篇:pugb怎么换服务器?

下一篇:创意建筑公司命名,寓意深远,独具匠心!