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

题目网址

自测-1 打印沙漏(20 point(s))

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

这里写图片描述

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

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

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

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

输入样例:

19 *

输出样例:

这里写图片描述

思路:计算通项公式

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    int x;//x表示输入的值
    char c;//c表示所给定的符号
    cin>>x>>c;

    double n;
    int xx = x;
    n = sqrt((x + 1) / 2);//通项公式为1+2(3+5+7+...) = 2n^2-1 = x
    while(n != (int)n)
    {
        xx--;
        n = sqrt((xx + 1) / 2);
    }

    int space = 0;//空格数
    for(int i = 2 * n - 1; i >= 3; i-=2)//打印上半部分沙漏
    {
        if(space)
            for(int j = 0; j < space; j++)
                printf(" ");
        for(int j = 0; j < i; j++)
            printf("%c", c);
        printf("\n");
        space++;
    }

    for(int j = 0; j < space; j++)
        printf(" ");
    printf("%c\n", c);//打印沙漏中间的1个符号
    space--;

    for(int i = 3; i <= 2 * n - 1; i+=2)//打印下半个沙漏
    {
        if(space)
            for(int j = 0; j < space; j++)
                printf(" ");
        for(int j = 0; j < i; j++)
            printf("%c", c);
        printf("\n");
        space--;
    }

    int r = x - (2 * (int)n * (int)n - 1);//计算余数
    printf("%d", r);
    return 0;
}

自测-2 素数对猜想(20 point(s))

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

现给定任意正整数N(<10​5​​),请计算不超过N的满足猜想的素数对的个数。
输入格式:

输入在一行给出正整数N。
输出格式:

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

输入样例:

20

输出样例:

4

思路:使用快速素数筛选法筛出 105 内所有素数后计算素数对。
注:MAX的值应略大于最大给定N

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define MAX 100010

int prime[MAX], cnt;
bool num[MAX];

int main()
{
    //求10000以内的所有素数
    memset(num, 1, sizeof(num));
    num[0] = num[1] = 0;
    cnt = 1;
    for(int i = 2; i < MAX; i++)//快速素数筛法
    {
        if(num[i])
            prime[cnt++] = i;
        for(int j = 1; j < cnt && prime[j] * i < MAX; j++)//从2~MAX,用它们乘以比它们小的素数并筛去
            num[prime[j] * i] = 0;
    }

    //计算素数对
    int n, c = 0;
    cin>>n;
    for(int i = 2; prime[i] <= n; i++)
    {
        if(prime[i] - prime[i - 1] == 2)
            c++;
    }
    cout<<c<<endl;
    return 0;
}

自测-3 数组元素循环右移问题(20 point(s))

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

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

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

输入样例:

6 2
1 2 3 4 5 6

输出样例:

5 6 1 2 3 4

:emmm……偷了点懒没有按照题目要求模拟……

代码如下:

#include <iostream>

using namespace std;

int num[105];

int main()
{
    int n, m;
    cin>>n>>m;
    for(int i = 0; i < n; i++)
        cin>>num[i];

    int r = m % n;

    for(int i = n - r; i < n; i++)
        cout<<num[i]<<" ";

    for(int i = 0; i < n - r; i++)
    {
        cout<<num[i];
        if(i != n - r - 1)
            cout<<" ";
    }

    cout<<endl;

    return 0;
}

自测-4 Have Fun with Numbers(20 point(s))

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.
Sample Input:

1234567899

Sample Output:

Yes
2469135798

题目大意:输入一个数字,乘以2, 看输入的数字和乘2后的结果出现的数字是否相同。比如输入中出现了123456789,乘2后的结果为246913578,也出现了123456789.

unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

思路:因为输入的数据最大不超过20位,所以即使使用unsigned __int64也会超,只能手动写*2的代码,输入和结果保存在两个数组中,排序后比较。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char num[25];//方便输入
int num1[25], num2[25];//模拟计算用的原数组和两倍数组
int n[25];//用来保存*2后的结果

int main()
{
    cin>>num;

    int carry = 0;//进位
    int len = strlen(num);
    for(int i = len - 1; i >= 0; i--)
    {
        num1[i] = num[i] - '0';
        num2[i] = (num1[i] *2) % 10;
        if(carry)
        {
            num2[i]++;
            carry = 0;
        }
        if((num1[i] * 2) / 10)
            carry = 1;
        n[i] = num2[i];
    }

    if(carry)//若最高位有1则一定不相等,故不用比较。但注意输出最高位的1
        cout<<"No"<<endl<<"1";
    else//排序后比较两个数组
    {
        sort(num1, num1 + len);
        sort(num2, num2 + len);

        bool flag = true;
        for(int i = 0; i < len; i ++)
        {
          if(num1[i] != num2[i])
            {
                flag = false;
                cout<<"No"<<endl;
                break;
            }
        }
        if(flag)
            cout<<"Yes"<<endl;
    }
    for(int i = 0; i < len; i++)
        cout<<n[i];
    return 0;
}

自测-5 Shuffling Machine(20 point(s))

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.
Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

题目大意:根据输入的数组洗54张牌,比如样例中将第一张牌移到第36这个位置、第二张牌移到第52个位置、第三张牌移动到第37个位置……
输入的第一个数字表示按照这个数组洗牌的次数

思路:打表,模拟

#include <iostream>
#include <cstring>

using namespace std;

string s[25][60] = {"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"};
int num[60];

int main()
{
    int n, cnt = 0;
    cin>>n;
    for(int i = 0; i < 54; i++)
        cin>>num[i];
    while(n--)
    {
        for(int i = 0; i < 54; i++)
            s[cnt + 1][num[i] - 1] = s[cnt][i];
        cnt++;
    }
    for(int i = 0; i < 53; i++)
        cout<<s[cnt][i]<<" ";
    cout<<s[cnt][53];
    return 0;
}
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值