数据结构课前测试题

自测-1 打印沙漏(20 分)

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

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

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

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

输入格式:

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

输出格式:

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

输入样例:

19 *

输出样例:

*****
 ***
  *
 ***
*****
2

题解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); // 读取最多总数N
        String symbol = sc.next(); // 读取符号
        int n = (int) Math.sqrt((N + 1) / 2); 
/*
假设一个完整三角形n行,那么总数为
1+2*3+2*5+...+2*(2n-1)=2n^2-1<=N
n<=sqrt((N+1)/2) 向下取整
*/
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(' ');
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(symbol);
            }
            System.out.println();
        } // 第一个完整倒三角形
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(' ');
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(symbol);
            }
            System.out.println();
        } // 第二个缺顶点正三角形
        System.out.print(N - 2 * n * n + 1); // 输出余数
    }
}

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

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

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

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:
20
输出样例:
4

import java.util.Scanner;

public class Main {
    public static boolean IsPrime(int num) {
        int bound = (int) Math.sqrt(num);
        for (int i = 2; i <= bound; i++)
            if (num % i == 0) {
                return false;
            }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int count = 0;
        for (int i = 2; i < N - 1; i++)
            if (IsPrime(i) && IsPrime(i + 2)) {
                count++;
            }
        System.out.println(count);
    }
}

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

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

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

输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int array[] = new int[100];
        int n = sc.nextInt();
        int m = sc.nextInt();

        for (int i = 0; i < n; i++)
            array[i] = sc.nextInt();

        int circle = m % n;
//注意到循环一圈的话,相当于数组元素没有移动,
//因此可以利用模运算来尽可能的减少数据移动的次数

        for (int i = 0; i < circle; i++) {    //移动次数
            int temp = array[n - 1];//temp为数组最后一个数
            for (int j = n - 2; j >= 0; j--)
                array[j + 1] = array[j];  //从后向前移动一次
            array[0] = temp;
        }

        for (int i = 0; i < n; i++) {
            if (i == 0)
                System.out.print(array[i]);
            else
                System.out.print(" " + array[i]);
        }

    }
}

00-自测4. Have Fun with Numbers (20)

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 file 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
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String numbers = sc.next();
        int n = numbers.length();
        int num[] = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = numbers.charAt(i) - 48; //按顺序读取字符,-48得到值
        }
        // System.out.println(num[0]);
        int newnum[] = new int[n]; // 创建新数组,和原来一样大小,如果超出,额外输出1
        boolean addone = false;
        boolean top = false;
        int tmp;
        for (int i = n - 1; i >= 0; i--) {
            if (addone) {
                tmp = num[i] * 2 + 1;
                addone = false;
            } else {
                tmp = num[i] * 2;
            }
            if (tmp >= 10) {
                addone = true;
                newnum[i] = tmp - 10;
                if (i == 0) {
                    top = true;
                }
            } else {
                newnum[i] = tmp;
            }
        }
        if (top) { //超出的情况
            System.out.println("No");
            System.out.print(1);
            for (int i = 0; i < n; i++) {
                System.out.print(newnum[i]);
            }
        } else { //两个数组先排序再一次比较
            boolean flag = true;
            int copynewnum[]=newnum.clone(); //保留原数组
            Arrays.sort(num);
            Arrays.sort(newnum);
            for (int i = 0; i < n; i++) {
                if(num[i]!=newnum[i]) {
                    flag = false;
                }
            }
            if(flag) {
                System.out.println("Yes");
            }else {
                System.out.println("No");
            }
            for (int i = 0; i < n; i++) {
                System.out.print(copynewnum[i]);
            }
        }

    }

}

00-自测5. Shuffling Machine (20)

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
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int repeat = sc.nextInt();  // 读取重复数
        int rule[] = new int[55];  
        int tmp[] = new int[55]; // 临时数组
        for(int i = 1; i <= 54; i++) {
            rule[i] = sc.nextInt(); // 读取规则列表,0号不用
        }
        int init[] = new int[55];
        for(int i = 1; i <= 54; i++) {
            init[i] = i;   // 初始值,未乱序之前
        }
        while(repeat!=0) { //开始乱序
            for(int i=1; i<=54;i++) {
                tmp[rule[i]] =  init[i];  //临时数组的  等于初始值的第i位置
            }
            for(int i=1; i<=54; i++) {
                init[i] =tmp[i];  // 将临时数组复制给初始数组
            }
            repeat--;
        }
        for(int i=1; i<=54; i++) {
            if(init[i]==54)System.out.print("J2");
            if(init[i]==53)System.out.print("J1");
            if(init[i]>=1&&init[i]<=13)System.out.print("S"+init[i]);
            if(init[i]>=14&&init[i]<=26)System.out.print("H"+(init[i]-13));
            if(init[i]>=27&&init[i]<=39)System.out.print("C"+(init[i]-26));
            if(init[i]>=40&&init[i]<=52)System.out.print("D"+(init[i]-39));
            if(i!=54)System.out.print(" ");
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,497评论 0 23
  • 阴天,冷, 天亮做完工。 遛狗的人也晚起, 没听到狗叫声,安静。 扫帚划过地面的节奏, 哗-哗--,缓慢, 每个季...
    爱稳阅读 324评论 0 0
  • 有时候怎么就那么讨厌你呢。把我当猴子耍呢。还想着大学四年有个好朋友挺好的呢。
    最强175阅读 141评论 0 0
  • 文/海煦 信仰 是心中不败的玫瑰 信仰 是心中不灭的灯光 信仰 是浅吟低唱的赞歌 信仰 是悲天悯人的思索 ...
    海煦阅读 344评论 3 11
  • 现代和弦的变化 音乐中的:Do Re Mi Fa Sol La Si 称之为:主音 上主音 中音 下属音 属音 下...
    吉他范儿阅读 956评论 0 6